LinuxCommandLibrary

rustc

Compile Rust code into executables

TLDR

Compile a binary crate

$ rustc [path/to/main.rs]
copy

Compile with optimizations (s means optimize for binary size; z is the same with even more optimizations)
$ rustc [[-C|--codegen]] lto [[-C|--codegen]] opt-level=[0|1|2|3|s|z] [path/to/main.rs]
copy

Compile with debugging information
$ rustc -g [path/to/main.rs]
copy

Explain an error message
$ rustc --explain [error_code]
copy

Compile with architecture-specific optimizations for the current CPU
$ rustc [[-C|--codegen]] target-cpu=[native] [path/to/main.rs]
copy

Display the target list (Note: You have to add a target using rustup first to be able to compile for it)
$ rustc --print target-list
copy

Compile for a specific target
$ rustc --target [target_triple] [path/to/main.rs]
copy

SYNOPSIS

rustc [OPTIONS] INPUT...

Examples:
rustc main.rs
rustc --crate-type lib my_lib.rs
cargo build (most common usage, where Cargo invokes rustc)

PARAMETERS

--help
    Display help information about the command, including all available options and their usage.

--version
    Print rustc's version, commit hash, and host triple (e.g., x86_64-unknown-linux-gnu).

-O, --optimize
    Controls the optimization level during compilation. For example, -O is an alias for -C opt-level=2 (a good balance of speed and compile time), or -C opt-level=s (optimize for size).

-C FLAG=VALUE, --codegen FLAG=VALUE
    Pass an arbitrary code generation option to the backend. Common flags include opt-level, debuginfo, lto (Link Time Optimization), and target-cpu.

--crate-type TYPE
    Specify the type of crate to be built. Common types are bin (executable), lib (Rust library), cdylib (C-compatible dynamic library), and staticlib (C-compatible static library).

--emit TYPE
    Specify what output files to emit. Can be a comma-separated list. Examples: asm (assembly), llvm-ir, obj (object file), link (default, produces executable/library).

--target TRIPLE
    Compile for a specific target triple (e.g., x86_64-unknown-linux-gnu for Linux, x86_64-pc-windows-msvc for Windows). Useful for cross-compilation.

--out-dir DIR
    Write all output files (executable, libraries, intermediate files) to the specified directory instead of the current working directory.

-L PATH, --extern CRATE=PATH
    -L adds a directory to the library search path. --extern explicitly links an external crate by specifying its name and path to its .rlib or .so file.

-g, --debug
    Emit debug information for the compiled binary, making it easier to debug with tools like GDB. This is equivalent to -C debuginfo=2.

--edition YEAR
    Specify the Rust edition to use for compilation (e.g., 2015, 2018, 2021). Editions allow Rust to evolve without breaking existing code.

DESCRIPTION

rustc is the official compiler for the Rust programming language. It takes Rust source code files (typically with a .rs extension) and translates them into machine code, producing executable binaries, static libraries, or dynamic libraries. While direct invocation of rustc is possible, most Rust developers interact with it indirectly through the Cargo build system, which handles project management, dependency resolution, and orchestrates the compilation process.

rustc is designed for high performance, memory safety, and concurrency, leveraging advanced optimization techniques and strict compile-time checks to prevent common programming errors like null pointer dereferences and data races. It offers a wide range of configuration options for optimization levels, code generation targets, and output formats, making it a powerful tool for low-level systems programming.

CAVEATS

While rustc is the core compiler, it's generally not used directly for day-to-day development in Rust. The Cargo build tool is the standard way to manage Rust projects, handle dependencies, run tests, and orchestrate the compilation process. Direct rustc usage is typically reserved for highly specialized scenarios, debugging build issues, or understanding compilation internals.

INCREMENTAL COMPILATION

rustc supports incremental compilation, which significantly speeds up re-compilations after small changes by reusing artifacts from previous builds. This feature is enabled by default when using Cargo, drastically reducing build times for iterative development.

TARGET TRIPLES

rustc uses a "target triple" (e.g., x86_64-unknown-linux-gnu) to identify the target architecture, vendor, operating system, and ABI for which the code is being compiled. This precise specification enables seamless cross-compilation for various platforms, from embedded systems to different operating systems.

HISTORY

The Rust programming language and its compiler, rustc, were initially developed at Mozilla Research, with the first public announcement in 2010. Rust 1.0, and thus the first stable version of rustc, was released in May 2015. Since then, rustc has been continuously developed by a large open-source community, adopting a rapid six-week release cycle. Its design reflects a strong emphasis on performance, memory safety, and concurrency, making Rust suitable for systems programming. Over time, features like incremental compilation and advanced optimization passes have been integrated into rustc to improve developer experience and compiled code quality.

SEE ALSO

cargo(1): The Rust package manager and build tool., rustup(1): The Rust toolchain installer and manager., gdb(1): The GNU Debugger, often used for debugging Rust executables., ld(1): The GNU linker, used by rustc to link object files.

Copied to clipboard