![](/img/trans.png)
[英]Why does this Rust 2018 code compile with `cargo build` but not using rustc?
[英]Why should I use Cargo if “cargo build” is slower than running rustc directly?
Cargo不是編譯器,而是程序包管理器。 它運行
rustc
並做了一些額外的工作(例如,解決依賴關系),因此它不會比裸rustc
快。
您可以通過運行cargo build --verbose
自己查看此信息,該命令輸出cargo運行的rustc
命令:
$ cargo build --verbose
Compiling hw v0.1.0 (file:///private/tmp/hw)
Running `rustc --crate-name hw src/main.rs --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=3c693c67d55ff970 -C extra-filename=-3c693c67d55ff970 --out-dir /private/tmp/hw/target/debug/deps -L dependency=/private/tmp/hw/target/debug/deps`
Finished dev [unoptimized + debuginfo] target(s) in 0.30 secs
為什么我仍要使用貨物
上面的輸出顯示了一個原因:查看傳遞給rustc
所有這些參數。 你知道他們每個人做什么嗎? 你想知道嗎 Cargo提取了一些細節,使您可以專注於代碼。
Cargo的功能還不止是調用編譯器 。 對大多數人來說,最大的好處是它可以根據版本管理您的依賴關系,並允許發布自己的包裝箱 。 它還允許在主編譯之前運行的構建腳本 。 它具有運行測試和示例的簡便方法。
更直接有用的是,Cargo執行檢查以查看是否應該完全重建 :
$ time rustc src/main.rs
0:00.21
$ time rustc src/main.rs
0:00.22
$ time cargo build
0:00.41
$ time cargo build
0:00.09 # Much better!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.