Rust Reference ยท Tools
The rustc command
Reference for rustc, the Rust compiler: compiling a single file, --edition, --crate-type, -C opt-level, --emit, --cfg, --explain and error codes.
rustc is the Rust compiler. It takes the root file of a crate, compiles that file and every module it declares, and produces an executable or a library. Most projects never call it directly because cargo build runs it for you with the right flags, but it is useful for single-file experiments, for reading what the compiler can emit, and for understanding what Cargo passes along.
This site compiles each runnable example with rustc --edition 2024 -C opt-level=1 -C debug-assertions=on -C overflow-checks=on. The command-line parts of this page are read-only: the examples below show what to type in your own terminal.
rustc main.rs # produces ./main (main.exe on Windows) rustc --edition 2024 -O main.rs # optimized ./main
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
rustc [options] input.rs
| Option | Description |
|---|---|
--edition 2015 or 2018 or 2021 or 2024 |
Language edition. The default for plain rustc is 2015, so always pass it (Cargo does it from Cargo.toml). |
-o file |
Name of the output file. |
--crate-type bin or lib or rlib or dylib or cdylib or staticlib or proc-macro |
What to produce. A file with fn main defaults to bin. |
--crate-name name |
Crate name (default: the file name). |
-O |
Shorthand for -C opt-level=3. |
-C opt-level=0 or 1 or 2 or 3 or s or z |
Optimization level; s and z optimize for size. |
-C debug-assertions=on or off |
Enables debug_assert! and cfg(debug_assertions). |
-C overflow-checks=on or off |
Panic on integer overflow. |
-C panic=unwind or abort |
Panic strategy. |
-g |
Include debug info. |
--emit asm,llvm-ir,mir,obj,link |
Write intermediate output instead of or besides the binary. |
--test |
Build the test harness for #[test] functions. |
--cfg name / --cfg 'feature="x"' |
Set a configuration flag for #[cfg]. |
--check-cfg ... |
Declare the valid cfg names so that typos are linted. |
-L path, --extern name=path |
Where to find libraries and how to name an external crate. |
-A, -W, -D, -F lint |
Allow, warn, deny or forbid a lint; -D warnings turns all warnings into errors. |
--explain E0382 |
Print a long explanation of an error code. |
--print cfg or target-list or sysroot |
Show compiler information. |
--target triple |
Cross-compile (the standard library for that target must be installed). |
-V, --version, -vV |
Print the version; -vV adds the commit hash and LLVM version. |
Usage notes
- Pass
--editionexplicitly. Without itrustcassumes the oldest edition, and newer keywords such asasyncorletchains behave differently or are rejected. - Without optimization (
-C opt-level=0, the default) code is slow but compiles fast. Benchmark only with-Oor Cargo's--release. - Error messages end with a code like
E0382.rustc --explain E0382prints a longer description with examples, and the same text is available online. - A module declared with
mod name;is looked up asname.rsorname/mod.rsnext to the root file;rustcfollows these automatically, but it compiles only the one crate you name. --emit=asmis a quick way to see what the optimizer did to a small function. Mark the function#[no_mangle]orpubto keep it from being removed.rustcaccepts@filearguments andRUSTFLAGSis a Cargo variable, not arustcone: Cargo passes its contents on torustc.- Libraries built with
--crate-type libare.rlibfiles that only the same compiler version can read; there is no stable Rust ABI. - Rust 1.99 on this site means the 2024 edition is the natural choice for new code.