簡體   English   中英

如果“貨物建造”比直接運行rustc慢,為什么要使用Cargo?

[英]Why should I use Cargo if “cargo build” is slower than running rustc directly?

我創建了一個簡單的hello world程序:

fn main() {
    println!("Hello, world");
}

當使用rustc vs cargo build編譯代碼時,cargo命令顯得較慢。 cargo build需要1.6秒 ,而rustc需要1 rustc 請參閱屏幕快照右側的時間戳。

為什么是這樣? 為什么我仍要使用貨物?

正如帕維爾·斯特拉霍夫(Pavel Strakhov)所說

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM