Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We're going to create a new Rust project with a binary crate. I've called mine `scaffold` but you can name it whatever you want, as long as cargo accepts the name. By default this will be the name of the final program that we run on the CLI. ```rust cargo new scaffold ``` Inside of our new directory, we have a new `Cargo.toml` detailing our package as well as the edition of Rust we're using. ```rust ❯ cat Cargo.toml [package] name = "scaffold" version = "0.1.0" edition = "2021" \# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] ``` as well as a `src/main.rs` which we can run with `cargo run` for the first time. ```rust ❯ cargo run Compiling scaffold v0.1.0 (/rust-adventure/scaffold) Finished dev [unoptimized + debuginfo] target(s) in 0.69s Running `target/debug/scaffold` Hello, world! ``` This creates a `target` directory that contains our build artifacts, as well as a `Cargo.lock` file which would include dependency information if we had any. ```rust ❯ cat Cargo.lock version = 3 [[package]] name = "scaffold" version = "0.1.0" ``` There is also a `.gitignore` file that ignores the `target/` directory, as we don't want to commit our build artifacts to git. We know we're going to use the [clap](https://docs.rs/clap/4.3.17/clap/index.html) crate with the `[derive](https://docs.rs/crate/clap/latest/features)` cargo feature. We'll cover what the derive feature is doing for us in the next lesson. Use `cargo add`, which is a built-in feature of cargo, to add clap to our project. The `-F` flag is used to enable cargo features, in this case the `derive` feature. Other features and whether or not they're enabled show in the results. ```rust ❯ cargo add clap -F derive Updating crates.io index Adding clap v4.3.19 to dependencies. Features: + color + derive + error-context + help + std + suggestions + usage - cargo - debug - deprecated - env - string - unicode - unstable-doc - unstable-styles - unstable-v5 - wrap_help Updating crates.io index ``` A cargo feature is used to conditionally add dependencies, or conditionally add swaths of code for a crate. In this case, the `derive` feature enables the code that powers a set of attribute macros that we'll explore in the next lesson. `clap` then appears in the `dependencies` array of our `Cargo.toml`, with the version specified and the `derive` feature in the `features` list. ```rust [package] name = "scaffold" version = "0.1.0" edition = "2021" [dependencies] clap = { version = "4.3.19", features = ["derive"] } ``` The `Cargo.lock` now also contains a whole host of detail about the transitive dependencies `clap` relies on. ```rust version = 3 [[package]] name = "anstream" version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ca84f3628370c59db74ee214b3263d58f9aadd9b4fe7e711fd87dc452b7f163" dependencies = [ "anstyle", "anstyle-parse", "anstyle-query", "anstyle-wincon", "colorchoice", "is-terminal", "utf8parse", ] ...more... ``` With clap added to our project, we can move on to using it.
- Loading branch information