rustc

Rust programming language compiler

TLDR

Compile a Rust file
$ rustc [main.rs]
Compile with output name
$ rustc [main.rs] -o [program]
Compile with optimizations
$ rustc -O [main.rs]
Compile in release mode
$ rustc -C opt-level=3 [main.rs]
Compile with debugging info
$ rustc -g [main.rs]
Show warnings
$ rustc -W warnings [main.rs]
Emit assembly
$ rustc --emit=asm [main.rs]
Emit LLVM IR
$ rustc --emit=llvm-ir [main.rs]
Check without compiling
$ rustc --emit=metadata [main.rs]
Show version
$ rustc --version

SYNOPSIS

rustc [options] input

DESCRIPTION

rustc is the compiler for the Rust programming language. It compiles Rust source code (.rs files) into executables or libraries. The compiler performs type checking, borrow checking, and optimization.While rustc can be used directly, most Rust development uses Cargo which invokes rustc with appropriate settings. Direct rustc usage is common for simple programs, learning, or advanced build customization.rustc uses LLVM for code generation, providing excellent optimization and support for many target platforms.

PARAMETERS

-o file

Output filename.
-O
Optimize (equivalent to -C opt-level=3).
-g
Include debug information.
-C option
Codegen options.
-W lint
Set lint warning level.
-A lint
Allow lint.
-D lint
Deny lint (make it error).
--emit= type
Output type (asm, llvm-ir, llvm-bc, obj, link, metadata, dep-info, mir).
--crate-type= type
Crate type (bin, lib, rlib, dylib, cdylib, staticlib, proc-macro).
--edition= year
Rust edition (2015, 2018, 2021, 2024).
--test
Build a test harness.
--print info
Print compiler information (e.g., target-list, cfg, sysroot).
-F lint
Forbid lint (cannot be overridden).
--target= triple
Target platform.
--explain code
Explain an error code.
-L path
Add library search path.
--extern name=path
Specify external crate location.

INSTALL

sudo apt install rustc
sudo dnf install rust
sudo pacman -S rust
sudo apk add rust
sudo zypper install rust
brew install rust
nix profile install nixpkgs#rustc

CAVEATS

Direct rustc usage requires manual dependency management. Most projects should use Cargo instead. Edition differences may cause compatibility issues. Cross-compilation requires target toolchain installation.

HISTORY

rustc was developed as part of the Rust programming language project, started by Graydon Hoare at Mozilla in 2006. Rust reached version 1.0 in May 2015. The compiler was originally written in OCaml but was rewritten in Rust (self-hosted) by version 1.0. Development continues under the Rust Foundation, established in 2021.

SEE ALSO

cargo(1), rustup(1), rustfmt(1), clippy(1)