How Rust Code Runs: Cargo, rustc & Native Binaries
Prerequisites: Your first Rust program
Rust is compiled ahead of time straight to native machine code.
There is no interpreter and no virtual machine, unlike Python, Java,
or C#. Behind the scenes Cargo calls rustc, the compiler, which uses
LLVM to generate machine code for your CPU.
cargo build # compile a debug build
./target/debug/hello # run it (target\debug\hello.exe on Windows)
Cargo puts everything it builds in a target/ folder. You'll use a
few commands constantly:
cargo run— build (if anything changed) and run the program.cargo build— build without running.cargo check— check the code for errors without producing a binary. It's much faster than a full build, so it's the quickest way to see compiler errors while you work.
Debug vs release builds
cargo build --release
./target/release/hello
- Debug (the default) — compiles quickly, includes debugging information, and adds extra checks such as panicking on integer overflow. The result is slower to run.
- Release (
--release) — takes longer to compile but applies full optimizations. The result can be many times faster. Use it for benchmarks and anything you ship.
No garbage collector
Many languages free unused memory with a garbage collector that runs periodically in the background. Rust doesn't have one. Instead, the compiler works out exactly when each value is no longer needed and inserts the code to free it at that point — this is ownership, covered later in this course. The result is predictable performance with no collection pauses.
Using rustc directly
You can compile a single file without Cargo:
rustc --edition 2024 main.rs
./main
That's fine for a quick experiment, but real projects always use Cargo, which handles dependencies, build settings, and tests for you.
A Rust binary includes the parts of the standard library it uses, so you can copy it to another machine with the same operating system and CPU architecture and run it without installing Rust. On Linux it usually still depends on the system C library, which is present on virtually every installation.