区别
为了更好地理解这些命令在高层次上的不同之处,我们可以比较这些命令的实现。所以我们首先将cargo repo 克隆为
git clone https://github.com/rust-lang/cargo.git
然后比较cargo build和cargo rustc的实现与
diff -u -w cargo/src/bin/cargo/commands/build.rs cargo/src/bin/cargo/commands/rustc.rs
由此不难看出cargo build和cargo rustc都是this内部cargo函数的薄包装:
pub fn compile<'a>(ws: &Workspace<'a>, options: &CompileOptions) -> CargoResult<Compilation<'a>> {
// ...
}
但在所公开的选项类型方面存在差异。事实上,您可以在cargo_compile.rs 顶部阅读定义该功能的这条评论:
//! This module contains the entry point for starting the compilation process
//! for commands like `build`, `test`, `doc`, `rustc`, etc.
换句话说,cargo build 和 cargo rustc 并不是围绕同一编译入口点的唯一瘦包装器。
为什么有不同的命令?
那么,为什么货物背后的人会选择这样做呢?为什么不使用一个带有大量标志的 cargo compile 命令来完全公开底层实现?
显然,必须以一种对 Rust 生态系统的各种目标受众来说易于记录和理解的方式组织事物。
例如,Rust 生态系统的大多数新手都不想接触到调整编译器标志的可能性。因此,在开始时仅将它们公开给cargo build 及其文档是有意义的,具有其特殊的编译选项风格。
一旦他们成为更高级的用户,他们会发现使用cargo rustc 可以对编译过程产生更详细的影响。