Anatomy of a Rust Program, Part I: Wink cargo.toml

This series of blog posts describe the structure of the wink command line program that I have written in rust and that I use to invoke other Windows and Linux programs from bash and Windows shells under Windows Subsystem for Linux. Especially as rust is well-documented and uses constructs much like many other languages, my goal is to describe the major structure of the program rather than every coding detail, including some elements specific to rust, but with minimal distracting links to documentation. This post introduces the project and its files.

I should start by repeating that I am not a programmer, and I am new to rust. I just started this learning project a few weeks ago. The following post from one of my other blogs provides a bit of background.

This code and some notes for this project are at github.

The important files are as follows.

I seem to be working against rust conventions by storing each type in a separate file, but because I am storing configuration in code and have configured over 450 things, one of my types has hundreds of lines (more than a thousand lines, depending on line length limits enforced by rustfmt). Either I am working against convention or rust uses constructs unfamiliar to me that merge the contents of multiple files into a single rust path (namespace).

If crates in packages other thank my wink program could use them, then /src/wsl.rs as well as the /src/wsl directory and all of its contents could belong in a separate library crate in a separate library package, typically created using cargo.

In the wink package (which is really a file system directory containing a .toml file and a /src directory with either a /src/main.rs file or a /src/lib.rs file or both):

  • /Cargo.toml: Defines the wink package, which contains one or more rust crates (cargo manages devops for rust).

I used the cargo new command to create the wink package. Under [package], name is important, as it determines the root name component of the path to library and binary crates in the package. Rust paths (namespaces) typically correspond to file system paths under the /src directory but also include paths in crates from external packages.

Under [dependencies], this package depends on crates for regular expressions, JSON serialization and deserialization, and coloring console output.

[package]
name = "wink"
version = "0.1.0"
authors = ["jw"]
edition = "2018"
include = ["src/**/*", "Cargo.toml"]

[dependencies]
regex = "1"
derive-new = "0.5"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
term = "*"

By convention, the rust compiler starts at /src/main.rs, which places an executable binary crate in the wink package, and includes /src/lib.rs, which places a library crate in the same package. These pieces of source code need to use rust path syntax with or without the use and mod keywords to reference any external crates, other crates and modules in this package, and even additional source code files in this package.

Running this package will call the main() function in /src/main.rs.

8 thoughts on “Anatomy of a Rust Program, Part I: Wink cargo.toml

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: