如何掌握Rust包管理器Cargo

這篇文章主要介紹“如何掌握Rust包管理器Cargo”,在日常操作中,相信很多人在如何掌握Rust包管理器Cargo問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何掌握Rust包管理器Cargo”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供屏南網(wǎng)站建設(shè)、屏南做網(wǎng)站、屏南網(wǎng)站設(shè)計、屏南網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、屏南企業(yè)網(wǎng)站模板建站服務(wù),十年屏南做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。

Rust 包管理器 Cargo 入門

了解 Rust 的軟件包管理器和構(gòu)建工具。  
-- Gaurav Kamathe(作者)

Rust 是一種現(xiàn)代編程語言,可提供高性能、可靠性和生產(chǎn)力。幾年來,它一直被 StackOverflow 調(diào)查評為最受歡迎的語言。

除了是一種出色的編程語言之外,Rust 還具有一個稱為 Cargo 的構(gòu)建系統(tǒng)和軟件包管理器。Cargo 處理許多任務(wù),例如構(gòu)建代碼、下載庫或依賴項等等。這兩者捆綁在一起,因此在安裝 Rust 時會得到 Cargo。

安裝 Rust 和 Cargo

在開始之前,你需要安裝 Rust 和 Cargo。Rust 項目提供了一個可下載的腳本來處理安裝。要獲取該腳本,請打開瀏覽器以訪問 https://sh.rustup.rs 并保存該文件。閱讀該腳本以確保你對它的具體行為有所了解,然后再運行它:

$ sh ./rustup.rs

你也可以參考這個安裝 Rust 的網(wǎng)頁以獲取更多信息。

安裝 Rust 和 Cargo 之后,你必須 獲取(source) env 文件中的配置:

$ source $HOME/.cargo/env

更好的辦法是,將所需目錄添加到 PATH 環(huán)境變量中:

export PATH=$PATH:~/.cargo/bin

如果你更喜歡使用軟件包管理器(例如 Linux 上的 DNF 或 Apt),請在發(fā)行版本的存儲庫中查找 Rust 和 Cargo 軟件包,并進(jìn)行相應(yīng)的安裝。 例如:

$ dnf install rust cargo

安裝并設(shè)置它們后,請驗證你擁有的 Rust 和 Cargo 版本:

$ rustc --version
rustc 1.41.0 (5e1a79984 2020-01-27)
$ cargo --version
cargo 1.41.0 (626f0f40e 2019-12-03)

手動構(gòu)建和運行 Rust

從在屏幕上打印“Hello, world!”的簡單程序開始。打開你喜歡的文本編輯器,然后鍵入以下程序:

$ cat hello.rs
fn main() {
    println!("Hello, world!");
}

以擴(kuò)展名 .rs 保存文件,以將其標(biāo)識為 Rust 源代碼文件。

使用 Rust 編譯器 rustc 編譯程序:

$ rustc hello.rs

編譯后,你將擁有一個與源程序同名的二進(jìn)制文件:

$ ls -l
total 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ù)期運行:

$ ./hello
Hello, world!

這些步驟對于較小的程序或任何你想快速測試的東西就足夠了。但是,在進(jìn)行涉及到多人的大型程序時,Cargo 是前進(jìn)的最佳之路。

使用 Cargo 創(chuàng)建新包

Cargo 是 Rust 的構(gòu)建系統(tǒng)和包管理器。它可以幫助開發(fā)人員下載和管理依賴項,并幫助創(chuàng)建 Rust 包。在 Rust 社區(qū)中,Rust 中的“包”通常被稱為“crate”(板條箱),但是在本文中,這兩個詞是可以互換的。請參閱 Rust 社區(qū)提供的 Cargo FAQ 來區(qū)分。

如果你需要有關(guān) Cargo 命令行實用程序的任何幫助,請使用 --help 或 -h 命令行參數(shù):

$ cargo –help

要創(chuàng)建一個新的包,請使用關(guān)鍵字 new,跟上包名稱。在這個例子中,使用 hello_opensource作為新的包名稱。運行該命令后,你將看到一條消息,確認(rèn) Cargo 已創(chuàng)建具有給定名稱的二進(jìn)制包:

$ cargo new hello_opensource
     Created binary (application) `hello_opensource` package

運行 tree 命令以查看目錄結(jié)構(gòu),它會報告已創(chuàng)建了一些文件和目錄。首先,它創(chuàng)建一個帶有包名稱的目錄,并且在該目錄內(nèi)有一個存放你的源代碼文件的 src 目錄:

$ tree .
.
└── hello_opensource
    ├── Cargo.toml
    └── src
        └── main.rs

2 directories, 2 files

Cargo 不僅可以創(chuàng)建包,它也創(chuàng)建了一個簡單的 “Hello, world” 程序。打開 main.rs 文件看看:

$ cat hello_opensource/src/main.rs
fn main() {
    println!("Hello, world!");
}

下一個要處理的文件是 Cargo.toml,這是你的包的配置文件。它包含有關(guān)包的信息,例如其名稱、版本、作者信息和 Rust 版本信息。

程序通常依賴于外部庫或依賴項來運行,這使你可以編寫應(yīng)用程序來執(zhí)行不知道如何編碼或不想花時間編碼的任務(wù)。你所有的依賴項都將在此文件中列出。此時,你的新程序還沒有任何依賴關(guān)系。打開 Cargo.toml 文件并查看其內(nèi)容:

$ cat hello_opensource/Cargo.toml
[package]
name = "hello_opensource"
version = "0.1.0"
authors = ["user <user@mail.com>"]
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òu)建一個二進(jìn)制文件(也稱為可執(zhí)行文件)。在此之前,進(jìn)入包目錄:

$ cd hello_opensource/

你可以使用 Cargo 的 build 命令來構(gòu)建包。注意消息說它正在“編譯”你的程序:

$ cargo build
   Compiling hello_opensource v0.1.0 (/opensource/hello_opensource)
    Finished dev [unoptimized + debuginfo] target(s) in 0.38s

運行 build 命令后,檢查項目目錄發(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-3pouh5i8ttpvz
                ├── 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.lock

9 directories, 17 files

哇!編譯過程產(chǎn)生了許多中間文件。另外,你的二進(jìn)制文件將以與軟件包相同的名稱保存在 ./target/debug 目錄中。

使用 Cargo 運行你的應(yīng)用程序

現(xiàn)在你的二進(jìn)制文件已經(jīng)構(gòu)建好了,使用 Cargo 的 run 命令運行它。如預(yù)期的那樣,它將在屏幕上打印 Hello, world!。

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/hello_opensource`
Hello, world!

或者,你可以直接運行二進(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_opensource
Hello, world!

假設(shè)你需要重建包,并丟棄早期編譯過程創(chuàng)建的所有二進(jìn)制文件和中間文件。Cargo 提供了一個方便的clean 選項來刪除所有中間文件,但源代碼和其他必需文件除外:

$ cargo clean
$ tree .
.
├── Cargo.lock
├── Cargo.toml
└── src
    └── main.rs

1 directory, 3 files

對程序進(jìn)行一些更改,然后再次運行以查看其工作方式。例如,下面這個較小的更改將 Opensource 添加到 Hello, world! 字符串中:

$ cat src/main.rs
fn main() {
    println!("Hello, Opensource world!");
}

現(xiàn)在,構(gòu)建該程序并再次運行它。這次,你會在屏幕上看到 Hello, Opensource world!

$ cargo build
   Compiling hello_opensource v0.1.0 (/opensource/hello_opensource)
    Finished dev [unoptimized + debuginfo] target(s) in 0.39s

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/hello_opensource`
Hello, Opensource world!

使用 Cargo 添加依賴項

Cargo 允許你添加程序需要運行的依賴項。使用 Cargo 添加依賴項非常容易。每個 Rust 包都包含一個 Cargo.toml 文件,其中包含一個依賴關(guān)系列表(默認(rèn)為空)。用你喜歡的文本編輯器打開該文件,找到 [dependencies] 部分,然后添加要包含在包中的庫。例如,將 rand 庫添加為依賴項:

$ cat Cargo.toml
[package]
name = "hello_opensource"
version = "0.1.0"
authors = ["test user <test@mail.com>"]
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)建你的包,看看會發(fā)生什么。

$ cargo build
    Updating crates.io index
   Compiling libc v0.2.66
   Compiling rand v0.4.6
   Compiling rand v0.3.23
   Compiling hello_opensource v0.1.0 (/opensource/hello_opensource)
    Finished dev [unoptimized + debuginfo] target(s) in 4.48s

現(xiàn)在,Cargo 會聯(lián)系 Crates.io(這是 Rust 用于存儲 crate(或包)的中央倉庫),并下載和編譯 rand。但是,等等 —— libc 包是怎么回事?你沒有要安裝 libc 啊。是的,rand 包依賴于 libc 包;因此,Cargo 也會下載并編譯 libc。

庫的新版本會不斷涌現(xiàn),而 Cargo 提供了一種使用 update 命令更新其所有依賴關(guān)系的簡便方法:

cargo update

你還可以選擇使用 -p 標(biāo)志跟上包名稱來更新特定的庫:

cargo update -p rand

使用單個命令進(jìn)行編譯和運行

到目前為止,每當(dāng)對程序進(jìn)行更改時,都先使用了 build 之后是 run。有一個更簡單的方法:你可以直接使用 run 命令,該命令會在內(nèi)部進(jìn)行編譯并運行該程序。要查看其工作原理,請首先清理你的軟件包目錄:

$ cargo clean
$ tree .
.
├── Cargo.lock
├── Cargo.toml
└── src
    └── main.rs

1 directory, 3 files

現(xiàn)在執(zhí)行 run。輸出信息表明它已進(jìn)行編譯,然后運行了該程序,這意味著你不需要每次都顯式地運行 build

$ cargo run
   Compiling hello_opensource v0.1.0 (/opensource/hello_opensource)
    Finished dev [unoptimized + debuginfo] target(s) in 0.41s
     Running `target/debug/hello_opensource`
Hello, world!

在開發(fā)過程中檢查代碼

在開發(fā)程序時,你經(jīng)常會經(jīng)歷多次迭代。你需要確保你的程序沒有編碼錯誤并且可以正常編譯。你不需要負(fù)擔(dān)在每次編譯時生成二進(jìn)制文件的開銷。Cargo 為你提供了一個 check 選項,該選項可以編譯代碼,但跳過了生成可執(zhí)行文件的最后一步。首先在包目錄中運行 cargo clean

$ tree .
.
├── Cargo.lock
├── Cargo.toml
└── src
    └── main.rs

1 directory, 3 files

現(xiàn)在運行 check 命令,查看對目錄進(jìn)行了哪些更改:

$ cargo check
    Checking hello_opensource v0.1.0 (/opensource/hello_opensource)
    Finished dev [unoptimized + debuginfo] target(s) in 0.18s

該輸出顯示,即使在編譯過程中創(chuàng)建了中間文件,但沒有創(chuàng)建最終的二進(jìn)制文件或可執(zhí)行文件。這樣可以節(jié)省一些時間,如果該包包含了數(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.lock

9 directories, 9 files

要查看你是否真的節(jié)省了時間,請對 build 和 check 命令進(jìn)行計時并進(jìn)行比較。首先,計時 build 命令:

$ time cargo build
   Compiling hello_opensource v0.1.0 (/opensource/hello_opensource)
    Finished dev [unoptimized + debuginfo] target(s) in 0.40s

real    0m0.416s
user    0m0.251s
sys     0m0.199s

在運行 check 命令之前清理目錄:

$ cargo clean

計時 check 命令:

$ time cargo check
    Checking hello_opensource v0.1.0 (/opensource/hello_opensource)
    Finished dev [unoptimized + debuginfo] target(s) in 0.15s

real    0m0.166s
user    0m0.086s
sys     0m0.081s

顯然,check 命令要快得多。

建立外部 Rust 包

到目前為止,你所做的這些都可以應(yīng)用于你從互聯(lián)網(wǎng)上獲得的任何 Rust crate。你只需要下載或克隆存儲庫,移至包文件夾,然后運行 build 命令,就可以了:

git clone <github-like-url>
cd <package-folder>
cargo build

使用 Cargo 構(gòu)建優(yōu)化的 Rust 程序

到目前為止,你已經(jīng)多次運行 build,但是你注意到它的輸出了嗎?不用擔(dān)心,再次構(gòu)建它并密切注意:

$ cargo build
   Compiling hello_opensource v0.1.0 (/opensource/hello_opensource)
    Finished dev [unoptimized + debuginfo] target(s) in 0.36s

看到了每次編譯后的 [unoptimized + debuginfo] 文本了嗎?這意味著 Cargo 生成的二進(jìn)制文件包含大量調(diào)試信息,并且未針對執(zhí)行進(jìn)行優(yōu)化。開發(fā)人員經(jīng)常經(jīng)歷開發(fā)的多次迭代,并且需要此調(diào)試信息進(jìn)行分析。同樣,性能并不是開發(fā)軟件時的近期目標(biāo)。因此,對于現(xiàn)在而言是沒問題的。

但是,一旦準(zhǔn)備好發(fā)布軟件,就不再需要這些調(diào)試信息。而是需要對其進(jìn)行優(yōu)化以獲得最佳性能。在開發(fā)的最后階段,可以將 --release 標(biāo)志與 build 一起使用。仔細(xì)看,編譯后,你應(yīng)該會看到 [optimized] 文本:

$ cargo build --release
   Compiling hello_opensource v0.1.0 (/opensource/hello_opensource)
    Finished release [optimized] target(s) in 0.29s

如果愿意,你可以通過這種練習(xí)來了解運行優(yōu)化軟件與未優(yōu)化軟件時節(jié)省的時間。

使用 Cargo 創(chuàng)建庫還是二進(jìn)制文件

任何軟件程序都可以粗略地分類為獨立二進(jìn)制文件或庫。一個獨立二進(jìn)制文件也許即使是當(dāng)做外部庫使用的,自身也是可以運行的。但是,作為一個庫,是可以被另一個獨立二進(jìn)制文件所利用的。到目前為止,你在本教程中構(gòu)建的所有程序都是獨立二進(jìn)制文件,因為這是 Cargo 的默認(rèn)設(shè)置。 要創(chuàng)建一個,請?zhí)砑?nbsp;--lib 選項:

$ cargo new --lib libhello
     Created library `libhello` package

這次,Cargo 不會創(chuàng)建 main.rs 文件,而是創(chuàng)建一個 lib.rs 文件。 你的庫的代碼應(yīng)該是這樣的:

$ tree .
.
└── libhello
    ├── Cargo.toml
    └── src
        └── lib.rs

2 directories, 2 files

Cargo 就是這樣的,不要奇怪,它在你的新庫文件中添加了一些代碼。通過移至包目錄并查看文件來查找添加的內(nèi)容。默認(rèn)情況下,Cargo 在庫文件中放置一個測試函數(shù)。

使用 Cargo 運行測試

Rust 為單元測試和集成測試提供了一流的支持,而 Cargo 允許你執(zhí)行以下任何測試:

$ cd libhello/

$ cat src/lib.rs
#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}

Cargo 有一個方便的 test 命令,可以運行代碼中存在的任何測試。嘗試默認(rèn)運行 Cargo 在庫代碼中放入的測試:

$ cargo test
   Compiling libhello v0.1.0 (/opensource/libhello)
    Finished test [unoptimized + debuginfo] target(s) in 0.55s
     Running target/debug/deps/libhello-d52e35bb47939653

running 1 test
test tests::it_works ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests libhello

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

深入了解 Cargo 內(nèi)部

你可能有興趣了解在運行一個 Cargo 命令時它底下發(fā)生了什么。畢竟,在許多方面,Cargo 只是個封裝器。要了解它在做什么,你可以將 -v 選項與任何 Cargo 命令一起使用,以將詳細(xì)信息輸出到屏幕。

這是使用 -v 選項運行 build 和 clean 的幾個例子。

在 build 命令中,你可以看到這些給定的命令行選項觸發(fā)了底層的 rustc(Rust 編譯器):

$ cargo build -v
   Compiling 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 -v
    Removing /opensource/hello_opensource/target

到此,關(guān)于“如何掌握Rust包管理器Cargo”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

當(dāng)前標(biāo)題:如何掌握Rust包管理器Cargo
URL分享:http://muchs.cn/article44/gpgpee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、自適應(yīng)網(wǎng)站手機網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計公司、網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)