A compiler turns source code into machine code and handles the bookkeeping needed to make it runnable by the operating system and CPU. Rust’s compiler is called rustc. section 2.1.1
Compilation does more than produce an executable:
- The compiler checks Rust’s rules before a program can run. Its strictness can catch problems during development rather than leaving them to cause a crash for users. section 1.8.3
- It optimizes a program’s size and speed. Rust also uses static method dispatch by default, giving the compiler opportunities to reduce call costs. section 1.7.1
Compile one file
Save a Rust program with a main() function as ok.rs. In a shell, run rustc ok.rs, then run the executable it creates. A successful compilation prints nothing; running this example prints OK. section 2.1.1
The commands show the distinction between compiling a file and running the executable produced from it. section 2.1.1
For a whole Rust crate, cargo manages the project, while rustc manages compilation. Running cargo run compiles and runs a project; cargo run --release produces an optimized release build. section 1.13 section 1.5








