Difference between rustc and cargo build commands

rustc command cargo build command
It is used to compile a single Rust source file or crate It is used to compile the entire Rust project and its dependencies
Example: rustc <source-file> or rustc <crate> Example: cargo build
It does not handle dependency management It handles dependency management through Cargo and the Cargo.toml file
You need to manage dependencies and configuration manually. Configuration is manged using the Cargo.toml file
It requires explicit compilation of dependencies It is automatically managed and compiles dependencies
It produces an executable or library file directly I produces build artifacts in the target directory
It does not perform incremental builds by default It performs incremental builds by default
rustc can manually specify optimization flags cargo build applies optimization based on the build profile in Cargo.toml
rustc does not execute build scripts cargo build executes build scripts specified in Cargo.toml
It relies on system Rust installation and environment It uses the project-specific Rust toolchain managed by Cargo


Documentations:

  1. https://doc.rust-lang.org/cargo/commands/cargo-build.html
  2. https://doc.rust-lang.org/book/ch01-03-hello-cargo.html

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!