Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Tools

cargo build and cargo run

Reference for cargo build and cargo run: debug and release profiles, the target directory, --bin, --example, --features, passing program arguments and flags.

cargo build compiles the current package and all of its dependencies. cargo run does the same and then starts the resulting binary. Both use the dev profile unless you ask for --release, which turns on optimizations and turns off overflow checks and debug assertions.

Build products go into a target/ directory next to Cargo.toml: target/debug/ for the dev profile and target/release/ for the release profile. Dependencies are compiled once and reused until their source, features or the compiler version change. Because the site cannot run Cargo, the commands are read-only; the runnable examples show how a program can detect the profile it was built with.

Terminal
cargo build               # target/debug/myapp
cargo build --release     # target/release/myapp
cargo run -- --name Ana   # arguments after -- go to your program

Try it

Rust Debug versus release behavior

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust Program arguments after --

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Terminal
cargo build [options]
cargo run [options] [-- args...]
Option Description
--release, -r Use the release profile (target/release).
--profile <name> Use a named profile, including custom ones.
--bin <name>, --lib, --example <name> Select the target; cargo run needs --bin or --example when there are several.
--package <name>, -p, --workspace Pick workspace members.
--features a,b, --all-features, --no-default-features Enable optional features.
--target <triple> Cross-compile; output goes to target/<triple>/.
--locked, --frozen, --offline Fail if Cargo.lock would change, or avoid the network.
-j <N>, --jobs Parallel compile jobs.
--manifest-path <file> Use a manifest elsewhere.
-v, -q, --message-format json Output control.
--timings Write an HTML report of build times.

Usage notes

  • Everything after -- in cargo run goes to your program, not to Cargo. cargo run --release -- --help shows your program's help.
  • Dev builds are slow at runtime. Do not benchmark them; use --release (or cargo bench).
  • Release builds wrap on integer overflow by default, while dev builds panic. You can change either with overflow-checks in the profile.
  • The first build compiles every dependency, which can take minutes. Later builds only recompile what changed.
  • Build scripts (build.rs) and procedural macros are compiled for the host machine and run during the build.
  • Cargo.lock pins exact dependency versions. Commit it for applications; use --locked in CI so the build never changes it silently.
  • Environment variable RUSTFLAGS adds flags to every rustc call. Changing it rebuilds everything.
  • cargo clean deletes target/; the folder can grow to several gigabytes, so add it to .gitignore (the default).