2.1 The Compilation Process: rustc
and Cargo
Like C, Rust is a compiled language. The Rust compiler, rustc
, translates Rust source code files (ending in .rs
) into executable binaries or libraries. However, the Rust ecosystem centers around Cargo, an integrated build system and package manager that significantly simplifies project management and compilation compared to traditional C workflows.
2.1.1 Cargo: Build System and Package Manager
Cargo acts as a unified frontend for compiling code, managing external libraries (called “crates” in Rust), running tests, generating documentation, and much more. It combines the roles often handled by separate tools like make
, cmake
, package managers (like apt
or vcpkg
for dependencies), and testing frameworks.
Creating and building a new Rust project with Cargo:
# Create a new binary project named 'my_project'
cargo new my_project
cd my_project
# Compile the project
cargo build
# Compile and run the project
cargo run
Cargo enforces a standard project layout (placing source code in src/
and project metadata, including dependencies, in Cargo.toml
), promoting consistency across Rust projects.