With Cargo, what do `cargo build`, `cargo run` and `--release` each do, and where does each one leave the compiled binary?
answer
- two built-in build modes
- dev profile versus release profile
- target/debug and target/release
- run builds first, then executes
- the double dash splits arguments
basics
~10 scargo build compiles the package with the dev profile into target/debug; cargo run does the same build and then executes the binary; --release switches either command to the optimized release profile and target/release.
solid answer
~40 s`cargo build` compiles the package and its dependencies with the **dev** profile, unoptimized and with debug info, and writes the binary to `target/debug/`. `cargo run` performs that same build, skipping it when nothing changed, and then executes the binary. Everything after `--` goes to the program, so `cargo run -- --verbose` reaches your code while `cargo run --verbose` makes Cargo itself verbose. Adding `--release` (or `-r`) selects the **release** profile, optimized and without debug info, and writes to `target/release/`; Cargo's status line names the profile it used, such as ``Finished `release` profile [optimized]``. Iterate with dev builds because they compile quickly, and measure or ship release builds, because an unoptimized binary can run many times slower.
code
rust · 8 linesuse std::env;
fn main() {
let args: Vec<String> = env::args().skip(1).collect();
println!("args = {args:?}");
let text = args.join(" ");
println!("{}", tally::count_words(&text));
}go deeper
Recall the two profiles and their two output folders, that cargo run builds before it runs, and that program arguments go after the double dash.
Explain what dev and release trade: compile speed and debuggability against runtime speed, and why each profile writes its own directory so switching never clobbers the other build.
Show the judgement of someone who has shipped: never measure target/debug, read the profile on Cargo's status line, and know that --target moves output under the triple's directory.
Speak to team conventions around these commands: which profile CI tests and ships, and whether a custom profile inheriting from release earns its place for builds that need debug info.
## Two commands, two default profiles Cargo is Rust's build tool, and the two commands every Rust developer types first are `cargo build` and `cargo run`. - **`cargo build`** compiles the current package and every dependency it needs. With no flags it uses the **dev** profile, whose defaults are `opt-level = 0`, `debug = true`, `debug-assertions = true` and `overflow-checks = true`: fast to compile, easy to debug, slow to run. - **`cargo run`** does exactly the same build, skipping it if nothing changed, and then starts the resulting executable. It is a convenience for the edit, compile and try loop. - **`--release`**, also spelled `-r`, switches either command to the **release** profile, whose defaults include `opt-level = 3` and `debug = false`. Compilation takes longer, and the program runs much faster. Cargo's final status line names the profile it used, for example ``Finished `dev` profile [unoptimized + debuginfo]`` or ``Finished `release` profile [optimized]``. Reading that line is the quickest way to answer "which build am I looking at?". What each profile setting means, and how to tune it, is the subject of Cargo's build profiles; this answer is about the commands that select them. ## Where the output goes Each profile writes into its own directory under `target/` at the root of the workspace, so switching between dev and release never overwrites the other build. | Command | Profile | Output directory | |---|---|---| | `cargo build`, `cargo run`, `cargo check` | `dev` | `target/debug/` | | `cargo build --release` | `release` | `target/release/` | | `cargo test` | `test` | `target/debug/` | | `cargo bench` | `bench` | `target/release/` | | `cargo build --profile ci` (a custom profile) | `ci` | `target/ci/` | | `cargo build --target wasm32-unknown-unknown` | `dev` | `target/wasm32-unknown-unknown/debug/` | The Cargo book notes that the dev and test profiles share `debug/` and release and bench share `release/` "for historical reasons". Passing `--target` puts everything under a directory named after the target triple. `--release` is shorthand for `--profile release`, and Cargo rejects the two together: `cargo build --release --profile ci` stops with "the argument '--release' cannot be used with '--profile <PROFILE-NAME>'". ## Passing arguments to your program A `cargo run` command line has two audiences, separated by `--`: 1. Options **before** `--` belong to Cargo: `--release`, `--bin NAME`, `-q`, `--verbose`, `--features`. 2. Everything **after** `--` is handed to the program unchanged, flags included. Plain words without a leading dash may also follow the options, as in `cargo run hello world`, but anything that looks like a flag is parsed by Cargo first. `cargo run --port 8080` therefore fails before your program starts, and Cargo's own tip points at the fix: "to pass '--port' as a value, use '-- --port'". The session in the code example shows both cases. `cargo run` also runs the program in your shell's **current working directory**, exactly as if you had typed the path yourself. That differs from `cargo test`, which sets the working directory to the package root. ## Choosing which binary runs When a package has one binary, `cargo run` runs it. With several, for example `src/main.rs` plus `src/bin/import.rs`, Cargo refuses to guess: "`cargo run` could not determine which binary to run." Pass `--bin import`, or `--example NAME` for an example, or set the `default-run` key in the manifest. How Cargo discovers those binary targets belongs to the manifest's target rules. ## Cleaning up `cargo clean` removes Cargo's build output. With no options it deletes the whole target directory; `--release` or `--profile NAME` removes one profile's directory, `-p NAME` cleans one package, `--doc` removes only the generated documentation, and `--dry-run` shows what would go. A clean build is rarely needed day to day, because Cargo's fingerprints already rebuild what changed, but it is the reset button when disk space or a stale artifact is the problem. ## Mistakes interviewers listen for - Timing, benchmarking or profiling a `target/debug` binary and reporting the numbers as real performance. - Believing `--release` only strips debug info; it changes the optimization level and turns off debug assertions and overflow checks as well. - Writing program flags before `--` and wondering why Cargo complains about them. - Shipping `target/debug/app` from a CI job because the build step forgot `--release`. - Expecting `cargo build --target ...` output in `target/debug/` instead of under the triple's directory.
- Why does `cargo run` fail after you add `src/bin/import.rs` to a package that already has `src/main.rs`?The package now has two binary targets, and Cargo will not guess which one to start: it reports that `cargo run` could not determine which binary to run and lists the available binaries. Pass `--bin import` or `--bin tally`, or set `default-run` in the `[package]` section so a plain `cargo run` has a default.
- Can you pass `--release` and `--profile ci` to the same Cargo command?No. `--release` is shorthand for `--profile release`, and Cargo rejects the pair with "the argument '--release' cannot be used with '--profile <PROFILE-NAME>'". If you want an optimized variant, define a custom profile with `inherits = "release"` and select it with `--profile ci` alone; its output goes to `target/ci/`.
- What working directory does a program get when you start it with `cargo run`?The current working directory of your shell, the same as running the binary by its path. Relative paths in the program are resolved from where you typed the command. `cargo test` behaves differently: it runs unit and integration tests with the package's root directory as the working directory.
- cargo build optimizes by default, so --release only removes debug info
- cargo run --verbose passes --verbose through to the program
- dev and release builds share one output folder and overwrite each other
- benchmark numbers from a target/debug binary represent production speed
- cargo run rebuilds the whole project from scratch every time