Using Cargo to Build, Check, or Run Your Project
Hands-on Rust — by Herbert Wolverson (17 / 120)
👈 Creating Your First Rust Program | TOC | Formatting Your Code 👉
You’ve already used Cargo to run your project, in Run Hello, World. Cargo offers a few other ways to interact with your program. You can type cargo help for a full list, or cargo [command] — help for detailed help about a command’s options. You can:
- Quickly check to see if a project is valid by typing cargo check. This checks your project and its dependencies for basic structural errors. This is typically a lot faster than a full build.
- Compile — but do not run — your project with cargo build.
- Remove the entire target directory (into which compiled code is placed) with cargo clean.
Cargo provides a few options to customize your build setup.
Debug and Release Builds
When you execute cargo run or cargo build, your project is built in debug mode. It contains very few optimizations, making it run a lot more slowly than it should. This makes it easier to debug and for Rust to tell you exactly what went wrong — but your program may run slowly. It also emits “debug information” — data your debugging tool can read to associate errors with lines of code. This makes for a much larger compiled program.