Rust 操作与命令 · 编译、运行、测试与基准测量至阅读文档与命令帮助
本轮仅静态审读,未执行工具链或示例命令。成对双花括号中的内容是待替换参数,短选项与长选项并列时表示任选一种写法,不要原样复制。下列命令会按用途安装、改写配置、编译或发布,运行前先确认当前目录、目标工具链与操作影响。
编译、运行、测试与基准测量
通常从 Cargo 构建和运行项目,随后测试和测量;需要直接传递编译器参数或独立编译时再看 cargo rustc、rustc。
cargo build · 编译本地软件包及其所有依赖项
更多信息:https://doc.rust-lang.org/cargo/commands/cargo-build.html。
- 在本地路径中构建由
Cargo.toml清单文件定义的一个或多个软件包:
cargo {{[b|build]}}- 以 release 模式构建,并进行优化:
cargo {{[b|build]}} {{[-r|--release]}}- 要求
Cargo.lock文件为最新版本:
cargo {{[b|build]}} --locked- 构建工作区中的所有软件包:
cargo {{[b|build]}} --workspace- 构建特定的软件包:
cargo {{[b|build]}} {{[-p|--package]}} {{软件包}}- 仅构建指定的二进制文件:
cargo {{[b|build]}} --bin {{名称}}- 仅构建指定的测试目标:
cargo {{[b|build]}} --test {{测试名称}}cargo run · 运行当前的 Cargo 软件包
注意: 执行二进制的工作目录将设置为当前工作目录。 更多信息:https://doc.rust-lang.org/cargo/commands/cargo-run.html。
- 运行默认的二进制目标:
cargo {{[r|run]}}- 运行指定的二进制文件:
cargo {{[r|run]}} --bin {{名称}}- 运行指定的示例:
cargo {{[r|run]}} --example {{示例名}}- 激活一系列以空格或逗号分隔的功能:
cargo {{[r|run]}} {{[-F|--features]}} "{{功能1 功能2 ...}}"- 禁用默认功能:
cargo {{[r|run]}} --no-default-features- 激活所有可用的功能:
cargo {{[r|run]}} --all-features- 使用指定的配置文件运行:
cargo {{[r|run]}} --profile {{名称}}cargo test · 执行 Rust 包的单元测试和集成测试
更多信息:https://doc.rust-lang.org/cargo/commands/cargo-test.html。
- 仅运行包含特定字符串在其名称中的测试:
cargo {{[t|test]}} {{测试名称}}- 设置并行运行测试用例的数量:
cargo {{[t|test]}} -- --test-threads {{数量}}- 在 release 模式下测试构建,启用优化:
cargo {{[t|test]}} {{[-r|--release]}}- 测试工作区中的所有包:
cargo {{[t|test]}} --workspace- 为特定包运行测试:
cargo {{[t|test]}} {{[-p|--package]}} {{包名}}- 运行测试时不隐藏测试执行的输出:
cargo {{[t|test]}} -- --nocapturecargo bench · 编译并执行基准测试
更多信息:https://doc.rust-lang.org/cargo/commands/cargo-bench.html。
- 执行软件包的所有基准测试:
cargo bench- 在基准测试失败时不停止:
cargo bench --no-fail-fast- 编译,但不运行基准测试:
cargo bench --no-run- 对指定的基准进行基准测试:
cargo bench --bench {{基准测试名称}}- 使用给定的配置文件进行基准测试(默认为
bench):
cargo bench --profile {{配置文件}}- 对所有示例目标进行基准测试:
cargo bench --examples- 对所有二进制目标进行基准测试:
cargo bench --bins- 对软件包的库(lib)进行基准测试:
cargo bench --libcargo rustc · 编译一个 Rust 软件包。类似于 cargo build,但您可以向编译器传递额外的选项
查看
rustc --help获取所有可用选项。 另请参阅:rustc。 更多信息:https://doc.rust-lang.org/cargo/commands/cargo-rustc.html。
- 构建软件包并向
rustc传递选项:
cargo rustc -- {{rustc_options}}- 在 release 模式下构建,启用优化:
cargo rustc {{[-r|--release]}}- 使用针对当前 CPU 的特定架构优化编译:
cargo rustc {{[-r|--release]}} -- {{[-C|--codegen]}} target-cpu=native- 使用速度优化编译:
cargo rustc -- {{[-C|--codegen]}} opt-level={{1|2|3}}- 使用体积([s]ize)优化编译(
z也会关闭循环向量化):
cargo rustc -- {{[-C|--codegen]}} opt-level={{s|z}}- 检查您的软件包是否使用了不安全的代码:
cargo rustc --lib -- -D unsafe-code- 构建特定的软件包:
cargo rustc {{[-p|--package]}} {{软件包}}- 仅构建指定的二进制文件:
cargo rustc --bin {{名称}}rustc · Rust 编译器
Rust 项目通常使用
cargo而不是直接调用rustc。 更多信息:https://doc.rust-lang.org/stable/rustc/。
- 编译二进制包(crate):
rustc {{路径/到/主文件.rs}}- 编译时启用优化(
s表示针对二进制大小优化;z含义相同但使用更多优化):
rustc {{[-C|--codegen]}} lto {{[-C|--codegen]}} opt-level={{0|1|2|3|s|z}} {{路径/到/主文件.rs}}- 编译时包含调试信息:
rustc {{[-g|--codegen debuginfo=2]}} {{路径/到/主文件.rs}}- 解释错误信息:
rustc --explain {{错误码}}- 编译时针对当前 CPU 架构启用特定优化:
rustc {{[-C|--codegen]}} target-cpu={{native}} {{路径/到/主文件.rs}}- 显示目标列表(注意:你必须先使用
rustup添加目标才能为其编译):
rustc --print target-list- 针对特定目标编译:
rustc --target {{目标三元组}} {{路径/到/主文件.rs}}- 显示帮助:
rustc {{[-h|--help]}}阅读文档与命令帮助
工具链文档与 Cargo 项目文档分别查阅 rustup doc、cargo doc;直接调用文档工具与命令帮助放在后面。
rustup doc · 打开当前工具链的离线 Rust 文档
这里没有列出所有文档页面。有关更多信息请参见
rustup help doc。 更多信息:https://rust-lang.github.io/rustup/。
- 打开主页:
rustup doc- 打开特定主题的文档(标准库中的模块、类型、关键字等):
rustup doc {{std::fs|usize|fn|...}}- 打开 the Rust Programming Language book:
rustup doc --book- 打开 the Cargo book:
rustup doc --cargo- 打开 the Rust Reference:
rustup doc --referencerustup man · 查看由 rustup 管理的命令的 man 手册页
- 查看默认工具链中某个命令的 man 手册页:
rustup man {{命令}}- 查看指定工具链中某个命令的 man 手册页:
rustup man --toolchain {{工具链}} {{命令}}rustup help · 显示 rustup 及其子命令的帮助信息
- 显示帮助:
rustup help- 显示子命令的帮助信息:
rustup help {{子命令}}cargo doc · 构建 Rust 软件包的文档
更多信息:https://doc.rust-lang.org/cargo/commands/cargo-doc.html。
- 为当前项目及所有依赖项构建文档:
cargo {{[d|doc]}}- 不为依赖项构建文档:
cargo {{[d|doc]}} --no-deps- 构建并在浏览器中打开文档:
cargo {{[d|doc]}} --open- 构建并查看特定软件包的文档:
cargo {{[d|doc]}} --open {{[-p|--package]}} {{软件包}}cargo rustdoc · 构建 Rust 软件包的文档
类似于
cargo doc,但您可以向rustdoc传递选项。查看rustdoc --help获取所有可用选项。 另请参阅:rustdoc。 更多信息:https://doc.rust-lang.org/cargo/commands/cargo-rustdoc.html。
- 向
rustdoc传递选项:
cargo rustdoc -- {{rustdoc_options}}- 关于文档 lint 发出警告:
cargo rustdoc -- {{[-W|--warn]}} rustdoc::{{lint_name}}- 忽略文档 lint:
cargo rustdoc -- {{[-A|--allow]}} rustdoc::{{lint_name}}- 为软件包的库生成文档:
cargo rustdoc --lib- 为指定的二进制文件生成文档:
cargo rustdoc --bin {{名称}}- 为指定的示例生成文档:
cargo rustdoc --example {{名称}}- 为指定的集成测试生成文档:
cargo rustdoc --test {{名称}}cargo help · 显示有关 cargo 及其子命令的帮助信息
更多信息:https://doc.rust-lang.org/cargo/commands/cargo-help.html。
- 显示一般帮助:
cargo help- 显示子命令的帮助信息:
cargo help {{子命令}}cargo version · 显示 cargo 版本信息
更多信息:https://doc.rust-lang.org/cargo/commands/cargo-version.html。
- 显示版本:
cargo version- 显示额外的构建信息:
cargo version {{[-v|--verbose]}}继续阅读
- 知识专题
- 知识专题
- 知识专题