新聞中心
了解 Rust 的軟件包管理器和構(gòu)建工具。
創(chuàng)新互聯(lián)建站堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的貴陽(yáng)網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
Rust 是一種現(xiàn)代編程語(yǔ)言,可提供高性能、可靠性和生產(chǎn)力。幾年來(lái),它一直被 StackOverflow 調(diào)查評(píng)為最受歡迎的語(yǔ)言。
除了是一種出色的編程語(yǔ)言之外,Rust 還具有一個(gè)稱為 Cargo 的構(gòu)建系統(tǒng)和軟件包管理器。Cargo 處理許多任務(wù),例如構(gòu)建代碼、下載庫(kù)或依賴項(xiàng)等等。這兩者捆綁在一起,因此在安裝 Rust 時(shí)會(huì)得到 Cargo。
安裝 Rust 和 Cargo
在開始之前,你需要安裝 Rust 和 Cargo。Rust 項(xiàng)目提供了一個(gè)可下載的腳本來(lái)處理安裝。要獲取該腳本,請(qǐng)打開瀏覽器以訪問 https://sh.rustup.rs 并保存該文件。閱讀該腳本以確保你對(duì)它的具體行為有所了解,然后再運(yùn)行它:
$ sh ./rustup.rs
你也可以參考這個(gè)安裝 Rust 的網(wǎng)頁(yè)以獲取更多信息。
安裝 Rust 和 Cargo 之后,你必須獲取source env 文件中的配置:
$ source $HOME/.cargo/env
更好的辦法是,將所需目錄添加到 PATH 環(huán)境變量中:
export PATH=$PATH:~/.cargo/bin
如果你更喜歡使用軟件包管理器(例如 Linux 上的 DNF 或 Apt),請(qǐng)?jiān)诎l(fā)行版本的存儲(chǔ)庫(kù)中查找 Rust 和 Cargo 軟件包,并進(jìn)行相應(yīng)的安裝。 例如:
$ dnf install rust cargo
安裝并設(shè)置它們后,請(qǐng)驗(yàn)證你擁有的 Rust 和 Cargo 版本:
$ rustc --versionrustc 1.41.0 (5e1a79984 2020-01-27)$ cargo --versioncargo 1.41.0 (626f0f40e 2019-12-03)
手動(dòng)構(gòu)建和運(yùn)行 Rust
從在屏幕上打印“Hello, world!”的簡(jiǎn)單程序開始。打開你喜歡的文本編輯器,然后鍵入以下程序:
$ cat hello.rsfn main() {println!("Hello, world!");}
以擴(kuò)展名 .rs 保存文件,以將其標(biāo)識(shí)為 Rust 源代碼文件。
使用 Rust 編譯器 rustc 編譯程序:
$ rustc hello.rs
編譯后,你將擁有一個(gè)與源程序同名的二進(jìn)制文件:
$ ls -ltotal 2592-rwxr-xr-x. 1 user group 2647944 Feb 13 14:14 hello-rw-r--r--. 1 user group 45 Feb 13 14:14 hello.rs$
執(zhí)行程序以驗(yàn)證其是否按預(yù)期運(yùn)行:
$ ./helloHello, world!
這些步驟對(duì)于較小的程序或任何你想快速測(cè)試的東西就足夠了。但是,在進(jìn)行涉及到多人的大型程序時(shí),Cargo 是前進(jìn)的最佳之路。
使用 Cargo 創(chuàng)建新包
Cargo 是 Rust 的構(gòu)建系統(tǒng)和包管理器。它可以幫助開發(fā)人員下載和管理依賴項(xiàng),并幫助創(chuàng)建 Rust 包。在 Rust 社區(qū)中,Rust 中的“包”通常被稱為“crate”(板條箱),但是在本文中,這兩個(gè)詞是可以互換的。請(qǐng)參閱 Rust 社區(qū)提供的 Cargo FAQ 來(lái)區(qū)分。
如果你需要有關(guān) Cargo 命令行實(shí)用程序的任何幫助,請(qǐng)使用 --help 或 -h 命令行參數(shù):
$ cargo –help
要?jiǎng)?chuàng)建一個(gè)新的包,請(qǐng)使用關(guān)鍵字 new,跟上包名稱。在這個(gè)例子中,使用 hello_opensource 作為新的包名稱。運(yùn)行該命令后,你將看到一條消息,確認(rèn) Cargo 已創(chuàng)建具有給定名稱的二進(jìn)制包:
$ cargo new hello_opensourceCreated binary (application) `hello_opensource` package
運(yùn)行 tree 命令以查看目錄結(jié)構(gòu),它會(huì)報(bào)告已創(chuàng)建了一些文件和目錄。首先,它創(chuàng)建一個(gè)帶有包名稱的目錄,并且在該目錄內(nèi)有一個(gè)存放你的源代碼文件的 src 目錄:
$ tree ..└── hello_opensource├── Cargo.toml└── src└── main.rs2 directories, 2 files
Cargo 不僅可以創(chuàng)建包,它也創(chuàng)建了一個(gè)簡(jiǎn)單的 “Hello, world” 程序。打開 main.rs 文件看看:
$ cat hello_opensource/src/main.rsfn main() {println!("Hello, world!");}
下一個(gè)要處理的文件是 Cargo.toml,這是你的包的配置文件。它包含有關(guān)包的信息,例如其名稱、版本、作者信息和 Rust 版本信息。
程序通常依賴于外部庫(kù)或依賴項(xiàng)來(lái)運(yùn)行,這使你可以編寫應(yīng)用程序來(lái)執(zhí)行不知道如何編碼或不想花時(shí)間編碼的任務(wù)。你所有的依賴項(xiàng)都將在此文件中列出。此時(shí),你的新程序還沒有任何依賴關(guān)系。打開 Cargo.toml 文件并查看其內(nèi)容:
$ cat hello_opensource/Cargo.toml[package]name = "hello_opensource"version = "0.1.0"authors = ["user"] edition = "2018"# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html[dependencies]
使用 Cargo 構(gòu)建程序
到目前為止,一切都很順利?,F(xiàn)在你已經(jīng)有了一個(gè)包,可構(gòu)建一個(gè)二進(jìn)制文件(也稱為可執(zhí)行文件)。在此之前,進(jìn)入包目錄:
$ cd hello_opensource/
你可以使用 Cargo 的 build 命令來(lái)構(gòu)建包。注意消息說(shuō)它正在“編譯”你的程序:
$ cargo buildCompiling hello_opensource v0.1.0 (/opensource/hello_opensource)Finished dev [unoptimized + debuginfo] target(s) in 0.38s
運(yùn)行 build 命令后,檢查項(xiàng)目目錄發(fā)生了什么:
$ tree ..├── Cargo.lock├── Cargo.toml├── src│ └── main.rs└── target└── debug├── build├── deps│ ├── hello_opensource-147b8a0f466515dd│ └── hello_opensource-147b8a0f466515dd.d├── examples├── hello_opensource├── hello_opensource.d└── incremental└── hello_opensource-3pouh4i8ttpvz├── s-fkmhjmt8tj-x962ep-1hivstog8wvf│ ├── 1r37g6m45p8rx66m.o│ ├── 2469ykny0eqo592v.o│ ├── 2g5i2x8ie8zed30i.o│ ├── 2yrvd7azhgjog6zy.o│ ├── 3g9rrdr4hyk76jtd.o│ ├── dep-graph.bin│ ├── query-cache.bin│ ├── work-products.bin│ └── wqif2s56aj0qtct.o└── s-fkmhjmt8tj-x962ep.lock9 directories, 17 files
哇!編譯過(guò)程產(chǎn)生了許多中間文件。另外,你的二進(jìn)制文件將以與軟件包相同的名稱保存在 ./target/debug 目錄中。
使用 Cargo 運(yùn)行你的應(yīng)用程序
現(xiàn)在你的二進(jìn)制文件已經(jīng)構(gòu)建好了,使用 Cargo 的 run 命令運(yùn)行它。如預(yù)期的那樣,它將在屏幕上打印 Hello, world!。
$ cargo runFinished dev [unoptimized + debuginfo] target(s) in 0.01sRunning `target/debug/hello_opensource`Hello, world!
或者,你可以直接運(yùn)行二進(jìn)制文件,該文件位于:
$ ls -l ./target/debug/hello_opensource-rwxr-xr-x. 2 root root 2655552 Feb 13 14:19 ./target/debug/hello_opensource
如預(yù)期的那樣,它產(chǎn)生相同的結(jié)果:
$ ./target/debug/hello_opensourceHello, world!
假設(shè)你需要重建包,并丟棄早期編譯過(guò)程創(chuàng)建的所有二進(jìn)制文件和中間文件。Cargo 提供了一個(gè)方便的clean 選項(xiàng)來(lái)刪除所有中間文件,但源代碼和其他必需文件除外:
$ cargo clean$ tree ..├── Cargo.lock├── Cargo.toml└── src└── main.rs1 directory, 3 files
對(duì)程序進(jìn)行一些更改,然后再次運(yùn)行以查看其工作方式。例如,下面這個(gè)較小的更改將 Opensource 添加到 Hello, world! 字符串中:
$ cat src/main.rsfn main() {println!("Hello, Opensource world!");}
現(xiàn)在,構(gòu)建該程序并再次運(yùn)行它。這次,你會(huì)在屏幕上看到 Hello, Opensource world!:
$ cargo buildCompiling hello_opensource v0.1.0 (/opensource/hello_opensource)Finished dev [unoptimized + debuginfo] target(s) in 0.39s$ cargo runFinished dev [unoptimized + debuginfo] target(s) in 0.01sRunning `target/debug/hello_opensource`Hello, Opensource world!
使用 Cargo 添加依賴項(xiàng)
Cargo 允許你添加程序需要運(yùn)行的依賴項(xiàng)。使用 Cargo 添加依賴項(xiàng)非常容易。每個(gè) Rust 包都包含一個(gè) Cargo.toml 文件,其中包含一個(gè)依賴關(guān)系列表(默認(rèn)為空)。用你喜歡的文本編輯器打開該文件,找到 [dependencies] 部分,然后添加要包含在包中的庫(kù)。例如,將 rand 庫(kù)添加為依賴項(xiàng):
$ cat Cargo.toml[package]name = "hello_opensource"version = "0.1.0"authors = ["test user"] edition = "2018"# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html[dependencies]rand = "0.3.14"
試試構(gòu)建你的包,看看會(huì)發(fā)生什么。
$ cargo buildUpdating crates.io indexCompiling libc v0.2.66Compiling rand v0.4.6Compiling rand v0.3.23Compiling hello_opensource v0.1.0 (/opensource/hello_opensource)Finished dev [unoptimized + debuginfo] target(s) in 4.48s
現(xiàn)在,Cargo 會(huì)聯(lián)系 Crates.io(這是 Rust 用于存儲(chǔ) crate(或包)的中央倉(cāng)庫(kù)),并下載和編譯 rand。但是,等等 —— libc 包是怎么回事?你沒有要安裝 libc 啊。是的,rand 包依賴于 libc 包;因此,Cargo 也會(huì)下載并編譯 libc。
庫(kù)的新版本會(huì)不斷涌現(xiàn),而 Cargo 提供了一種使用 update 命令更新其所有依賴關(guān)系的簡(jiǎn)便方法:
cargo update
你還可以選擇使用 -p 標(biāo)志跟上包名稱來(lái)更新特定的庫(kù):
cargo update -p rand
使用單個(gè)命令進(jìn)行編譯和運(yùn)行
到目前為止,每當(dāng)對(duì)程序進(jìn)行更改時(shí),都先使用了 build 之后是 run。有一個(gè)更簡(jiǎn)單的方法:你可以直接使用 run 命令,該命令會(huì)在內(nèi)部進(jìn)行編譯并運(yùn)行該程序。要查看其工作原理,請(qǐng)首先清理你的軟件包目錄:
$ cargo clean$ tree ..├── Cargo.lock├── Cargo.toml└── src└── main.rs1 directory, 3 files
現(xiàn)在執(zhí)行 run。輸出信息表明它已進(jìn)行編譯,然后運(yùn)行了該程序,這意味著你不需要每次都顯式地運(yùn)行 build:
$ cargo runCompiling hello_opensource v0.1.0 (/opensource/hello_opensource)Finished dev [unoptimized + debuginfo] target(s) in 0.41sRunning `target/debug/hello_opensource`Hello, world!
在開發(fā)過(guò)程中檢查代碼
在開發(fā)程序時(shí),你經(jīng)常會(huì)經(jīng)歷多次迭代。你需要確保你的程序沒有編碼錯(cuò)誤并且可以正常編譯。你不需要負(fù)擔(dān)在每次編譯時(shí)生成二進(jìn)制文件的開銷。Cargo 為你提供了一個(gè) check 選項(xiàng),該選項(xiàng)可以編譯代碼,但跳過(guò)了生成可執(zhí)行文件的最后一步。首先在包目錄中運(yùn)行 cargo clean:
$ tree ..├── Cargo.lock├── Cargo.toml└── src└── main.rs1 directory, 3 files
現(xiàn)在運(yùn)行 check 命令,查看對(duì)目錄進(jìn)行了哪些更改:
$ cargo checkChecking hello_opensource v0.1.0 (/opensource/hello_opensource)Finished dev [unoptimized + debuginfo] target(s) in 0.18s
該輸出顯示,即使在編譯過(guò)程中創(chuàng)建了中間文件,但沒有創(chuàng)建最終的二進(jìn)制文件或可執(zhí)行文件。這樣可以節(jié)省一些時(shí)間,如果該包包含了數(shù)千行代碼,這非常重要:
$ tree ..├── Cargo.lock├── Cargo.toml├── src│ └── main.rs└── target└── debug├── build├── deps│ ├── hello_opensource-842d9a06b2b6a19b.d│ └── libhello_opensource-842d9a06b2b6a19b.rmeta├── examples└── incremental└── hello_opensource-1m3f8arxhgo1u├── s-fkmhw18fjk-542o8d-18nukzzq7hpxe│ ├── dep-graph.bin│ ├── query-cache.bin│ └── work-products.bin└── s-fkmhw18fjk-542o8d.lock9 directories, 9 files
要查看你是否真的節(jié)省了時(shí)間,請(qǐng)對(duì) build 和 check 命令進(jìn)行計(jì)時(shí)并進(jìn)行比較。首先,計(jì)時(shí) build 命令:
$ time cargo buildCompiling hello_opensource v0.1.0 (/opensource/hello_opensource)Finished dev [unoptimized + debuginfo] target(s) in 0.40sreal 0m0.416suser 0m0.251ssys 0m0.199s
在運(yùn)行 check 命令之前清理目錄:
$ cargo clean
計(jì)時(shí) check 命令:
$ time cargo checkChecking hello_opensource v0.1.0 (/opensource/hello_opensource)Finished dev [unoptimized + debuginfo] target(s) in 0.15sreal 0m0.166suser 0m0.086ssys 0m0.081s
顯然,check 命令要快得多。
建立外部 Rust 包
到目前為止,你所做的這些都可以應(yīng)用于你從互聯(lián)網(wǎng)上獲得的任何 Rust crate。你只需要下載或克隆存儲(chǔ)庫(kù),移至包文件夾,然后運(yùn)行 build 命令,就可以了:
git clonecdcargo build
使用 Cargo 構(gòu)建優(yōu)化的 Rust 程序
到目前為止,你已經(jīng)多次運(yùn)行 build,但是你注意到它的輸出了嗎?不用擔(dān)心,再次構(gòu)建它并密切注意:
$ cargo buildCompiling hello_opensource v0.1.0 (/opensource/hello_opensource)Finished dev [unoptimized + debuginfo] target(s) in 0.36s
看到了每次編譯后的 [unoptimized + debuginfo] 文本了嗎?這意味著 Cargo 生成的二進(jìn)制文件包含大量調(diào)試信息,并且未針對(duì)執(zhí)行進(jìn)行優(yōu)化。開發(fā)人員經(jīng)常經(jīng)歷開發(fā)的多次迭代,并且需要此調(diào)試信息進(jìn)行分析。同樣,性能并不是開發(fā)軟件時(shí)的近期目標(biāo)。因此,對(duì)于現(xiàn)在而言是沒問題的。
但是,一旦準(zhǔn)備好發(fā)布軟件,就不再需要這些調(diào)試信息。而是需要對(duì)其進(jìn)行優(yōu)化以獲得最佳性能。在開發(fā)的最后階段,可以將 --release 標(biāo)志與 build 一起使用。仔細(xì)看,編譯后,你應(yīng)該會(huì)看到 [optimized] 文本:
$ cargo build --releaseCompiling hello_opensource v0.1.0 (/opensource/hello_opensource)Finished release [optimized] target(s) in 0.29s
如果愿意,你可以通過(guò)這種練習(xí)來(lái)了解運(yùn)行優(yōu)化軟件與未優(yōu)化軟件時(shí)節(jié)省的時(shí)間。
使用 Cargo 創(chuàng)建庫(kù)還是二進(jìn)制文件
任何軟件程序都可以粗略地分類為獨(dú)立二進(jìn)制文件或庫(kù)。一個(gè)獨(dú)立二進(jìn)制文件也許即使是當(dāng)做外部庫(kù)使用的,自身也是可以運(yùn)行的。但是,作為一個(gè)庫(kù),是可以被另一個(gè)獨(dú)立二進(jìn)制文件所利用的。到目前為止,你在本教程中構(gòu)建的所有程序都是獨(dú)立二進(jìn)制文件,因?yàn)檫@是 Cargo 的默認(rèn)設(shè)置。 要?jiǎng)?chuàng)建一個(gè)庫(kù),請(qǐng)?zhí)砑?--lib 選項(xiàng):
$ cargo new --lib libhelloCreated library `libhello` package
這次,Cargo 不會(huì)創(chuàng)建 main.rs 文件,而是創(chuàng)建一個(gè) lib.rs 文件。 你的庫(kù)的代碼應(yīng)該是這樣的:
$ tree ..└── libhello├── Cargo.toml└── src└── lib.rs2 directories, 2 files
Cargo 就是這樣的,不要奇怪,它在你的新庫(kù)文件中添加了一些代碼。通過(guò)移至包目錄并查看文件來(lái)查找添加的內(nèi)容。默認(rèn)情況下,Cargo 在庫(kù)文件中放置一個(gè)測(cè)試函數(shù)。
使用 Cargo 運(yùn)行測(cè)試
Rust 為單元測(cè)試和集成測(cè)試提供了一流的支持,而 Cargo 允許你執(zhí)行以下任何測(cè)試:
$ cd libhello/$ cat src/lib.rs#[cfg(test)]mod tests {#[test]fn it_works() {assert_eq!(2 + 2, 4);}}
Cargo 有一個(gè)方便的 test 命令,可以運(yùn)行代碼中存在的任何測(cè)試。嘗試默認(rèn)運(yùn)行 Cargo 在庫(kù)代碼中放入的測(cè)試:
$ cargo testCompiling libhello v0.1.0 (/opensource/libhello)Finished test [unoptimized + debuginfo] target(s) in 0.55sRunning target/debug/deps/libhello-d52e35bb47939653running 1 testtest tests::it_works ... oktest result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered outDoc-tests libhellorunning 0 teststest result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
深入了解 Cargo 內(nèi)部
你可能有興趣了解在運(yùn)行一個(gè) Cargo 命令時(shí)它底下發(fā)生了什么。畢竟,在許多方面,Cargo 只是個(gè)封裝器。要了解它在做什么,你可以將 -v 選項(xiàng)與任何 Cargo 命令一起使用,以將詳細(xì)信息輸出到屏幕。
這是使用 -v 選項(xiàng)運(yùn)行 build 和 clean 的幾個(gè)例子。
在 build 命令中,你可以看到這些給定的命令行選項(xiàng)觸發(fā)了底層的 rustc(Rust 編譯器):
$ cargo build -vCompiling hello_opensource v0.1.0 (/opensource/hello_opensource)Running `rustc --edition=2018 --crate-name hello_opensource src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=147b8a0f466515dd -C extra-filename=-147b8a0f466515dd --out-dir /opensource/hello_opensource/target/debug/deps -C incremental=/opensource/hello_opensource/target/debug/incremental -L dependency=/opensource/hello_opensource/target/debug/deps`Finished dev [unoptimized + debuginfo] target(s) in 0.36s
而 clean 命令表明它只是刪除了包含中間文件和二進(jìn)制文件的目錄:
$ cargo clean -vRemoving /opensource/hello_opensource/target
不要讓你的技能生銹
要擴(kuò)展你的技能,請(qǐng)嘗試使用 Rust 和 Cargo 編寫并運(yùn)行一個(gè)稍微復(fù)雜的程序。很簡(jiǎn)單就可以做到:例如,嘗試列出當(dāng)前目錄中的所有文件(可以用 9 行代碼完成),或者嘗試自己回顯輸入。小型的實(shí)踐應(yīng)用程序可幫助你熟悉語(yǔ)法以及編寫和測(cè)試代碼的過(guò)程。
本文為剛起步的 Rust 程序員提供了大量信息,以使他們可以開始入門 Cargo。但是,當(dāng)你開始處理更大、更復(fù)雜的程序時(shí),你需要對(duì) Cargo 有更深入的了解。當(dāng)你準(zhǔn)備好迎接更多內(nèi)容時(shí),請(qǐng)下載并閱讀 Rust 團(tuán)隊(duì)編寫的開源的《Cargo 手冊(cè)》,看看你可以創(chuàng)造什么!
分享名稱:Rust包管理器Cargo入門
路徑分享:http://m.5511xx.com/article/djdspsg.html


咨詢
建站咨詢

