Sub topic 7
Building, running, and checking with Cargo
This chapter assumes you have installed Rust with rustup, confirmed that rustc and cargo run, and created a project with cargo new hello_cargo. You should be comfortable running commands in a terminal. Here you learn the three commands you will use most: cargo check, cargo build, and cargo run.
The three commands
Cargo is the build tool that drives the Rust compiler, rustc, for you. You rarely call rustc directly. Each of the three commands does a different amount of work:
| Command | What it does | Produces a binary? |
|---|---|---|
| `cargo check` | Type-checks the code and reports errors | No |
| `cargo build` | Compiles the code into an executable | Yes, in `target/debug` |
| `cargo run` | Builds if needed, then runs the executable | Yes, and runs it |
Run them from the project directory, the one that contains Cargo.toml. Cargo finds the manifest by looking in the current directory and then in its parents.
Running the project
The file src/main.rs that cargo new generates looks like this:
fn main() {
println!("Hello, world!");
}The main function is the entry point of the program. Start it with:
cargo runThe first time, Cargo prints a Compiling line and a Finished line, then the output of your program. Run it again and Cargo skips compilation, because nothing changed. The program prints Hello, world! either way.
Checking and building
While you edit code, you mostly want to know whether it compiles. cargo check answers that faster than a full build, because it skips generating machine code.
cargo check
cargo build
cargo build --releasecargo build writes the executable to target/debug/hello_cargo. On Windows the file is hello_cargo.exe. You can run it directly, without Cargo:
./target/debug/hello_cargoThe target directory holds all build output. It can become large and you can regenerate it at any time, so do not commit it to version control. cargo new already creates a .gitignore that excludes it when it initializes a Git repository.
How the commands relate
Reading compiler errors
Change println! to printl! and run cargo check. The compiler reports an error that names the file, the line, and the column, and often suggests a fix. Read the first error first. Later errors are often side effects of the first one.
fn main() {
printl!("Hello, world!");
}Restore the correct spelling before moving on.
Common errors
could not find Cargo.toml in ... or any parent directory: you ran Cargo outside a project. Usecdto move into the project directory.error: linker 'cc' not found(or a similar linker message): the system has no C linker. The installation chapter covers the fix for your platform.- Running
./target/debug/hello_cargofails with "No such file": you have not built yet. Runcargo buildfirst, or usecargo run, which builds for you. - Stale results after renaming the package: the binary takes its name from
nameinCargo.toml. If you rename it, look for the new file name intarget/debug.
Exercises
Exercise
You changed one line in src/main.rs and only want to know whether the project still compiles. Which command is the fastest choice, and why?
Show answer
cargo check. It type-checks the code and reports errors without generating an executable, so it finishes sooner than cargo build or cargo run.
Exercise
After cargo build succeeds in a project named hello_cargo, where is the executable on Linux or macOS, and how do you run it without Cargo?
Show answer
It is at target/debug/hello_cargo. Run it with ./target/debug/hello_cargo from the project directory.