← 返回命令列表

Linux command

rustc 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

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

说明

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.

参数

-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.

FAQ

What is the rustc command used for?

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.

How do I run a basic rustc example?

Run `rustc [main.rs]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -o _file_ do in rustc?

Output filename.