Cargo Run and Build
cargo build and cargo run are simple commands that turn your Rust code into a program and, if you want, run it right away.Building a Project
cargo build compiles your project without running it. The resulting debug executable is placed in target/debug/, and by default this build includes debug information and skips heavy optimizations so compilation itself is fast.
Example: Building a Project
cargo build
⚠️ Run this command in your terminal.
Running a Project
cargo run is a convenience command that compiles your project (only if source files changed) and then immediately executes the resulting binary. This is the command you will use most often while developing.
Example: Running a Project
cargo run
⚠️ Run this command in your terminal.
Release Builds
Passing --release tells Cargo to apply compiler optimizations, producing a smaller and much faster binary at the cost of longer compile times. Release builds are what you ship to production or use for performance benchmarking.
Example: Release Builds
cargo build --release
⚠️ Run this command in your terminal.
Seeing the Output in Code
Whether built in debug or release mode, the program's logic and output are identical -- only speed and binary size differ. This example is exactly what cargo run would execute after scaffolding a project.
Example: Seeing the Output in Code
fn main() {
println!("Built with cargo build, or run directly with cargo run");
}
Login to try C/C++/Java/PHP code in the editor
- Running
cargo buildand then manually hunting for the binary instead of just usingcargo runfor quick iteration. - Not realizing
cargo build --releaseproduces a much faster, optimized binary, and benchmarking with a debug build instead. - Deleting the
target/folder thinking it contains source code, when it is safe-to-delete build output.
cargo buildcompiles the project and places the executable undertarget/debug/.cargo runcompiles (if needed) and immediately runs the resulting binary in one step.cargo build --releaseproduces an optimized binary undertarget/release/, used for production/performance.- Cargo only recompiles files that changed, making repeated builds fast.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: