Initialize a new Cargo binary crate in a new package

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 3 years ago
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."

egghead
egghead
~ 32 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today