Rustc Compiler - Compile Rust Code

Compile Rust code efficiently with the Rustc compiler. This guide provides a comprehensive overview of Rustc commands and options for various compilation scenarios. Learn how to optimize for speed, size, and debugging.

Rustc Compiler

The Rust compiler (rustc) is a powerful tool for compiling Rust source code into executable binaries. This guide covers basic usage and common options.

Basic Compilation

rustc main.rs

This command compiles the main.rs file into an executable.

Compilation with Options

# Compile with debugging information
rustc -g main.rs

# Optimize for speed
rustc -O main.rs

# Optimize for size
rustc -C opt-level=s main.rs

# Emit only metadata
rustc --emit=metadata main.rs

# Check for errors without code generation
rustc --emit=metadata -Z no-codegen main.rs

# Specify target triple for cross-compilation
rustc --target=<target_triple> main.rs

# Specify output filename
rustc -o output_filename main.rs

# Produce LLVM IR or assembly
rustc --emit=llvm-ir main.rs
rustc --emit=asm main.rs

# Compile with conditional compilation flags
rustc --cfg <flag> main.rs

# Link with a specific library
rustc -l <library_name> main.rs

These examples demonstrate various options for customizing the compilation process. Refer to the official Rust documentation for a complete list of options.

Further Reading