cargo init
By default cargo initializes a new package containing a binary crate.
The file that gets compiled into the binary is located at
src/main.rs
. This is by convention and we can specify different paths
and names for binaries.
We also get a Cargo.lock
and a Cargo.toml
. These are similar to
JavaScript's package.json
and package-lock.json
or yarn.lock
.
The Cargo.toml
defines different features of our package including
dependencies
, defining zero or one library crates, and defining as
many binary crates as we want.
We can run the new binary with cargo run
.
❯ cargo run
Compiling digital-garden-steps v0.1.0 (/digital-garden-steps)
Finished dev [unoptimized + debuginfo] target(s) in 1.38s
Running `target/debug/digital-garden-steps`
Hello, world!
Chris Biscardi: [0:00] After sketching out some of our README, we'll want to initiate a new Cargo project. We'll use cargo-init for this. Cargo-init defaults to creating a new binary application. This creates a binary crate inside of our package. You can choose between initializing a binary crate and a library crate when using cargo-init.
[0:23] If we look at the file list, we can see a .gitignore, a src folder with a main.rs in it, and a Cargo.toml. If you're familiar with other ecosystems, such as npm, Cargo.toml will look familiar to you. Instead of a package.json, this file is written in TOML, but it contains the same information, such as the name of our package, the version, the authors, the edition of Rust that we're using, and then we can specify our dependencies, including our dev dependencies.
[0:55] The .gitignore contains a single entry, /target. /target is where all of our code is going to compile into whenever we do a cargo build or a cargo run. The code that we'll compile into our binary is located in src/main.rs. In this case, we have a small program with a single main() function that uses a println!() macro to print "Hello world."
[1:18] If we run cargo run, cargo will call out to rustc to compile our application and run it. In this case, cargo run selects the default binary. It compiles our package, DigitalGarden_v0.1., and my package is located in users/Chris/egghead/DigitalGarden. It'll tell us when it's done, and how long it took, and it'll tell us which version it's running.
[1:43] We compiled a debug release. Inside of target, inside of the debug folder, we'll find a binary called DigitalGarden that prints "Hello world."