1. 程式人生 > 其它 >rust: 引用第三方庫(Cargo.toml、Cargo.lock檔案)

rust: 引用第三方庫(Cargo.toml、Cargo.lock檔案)

技術標籤:Rustrust引用庫cargo.tomlcargo.lock

接下來我要使用隨機數生成函式,這個函式在 rand 庫中。我們在 Cargo.toml 檔案中的依賴項中,加入對 rand 的依賴說明。

[package]
name = "game"
version = "0.1.0"
authors = ["xuyeping"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] rand = "0.3.14"

“0.3.14” 說明我們要加入API與該版本號相容的第三方庫。執行 cargo build 命令,系統自動下載該庫程式碼。

>>cargo run
    Updating crates.io index
  Downloaded libc v0.2.81
  Downloaded rand v0.3.23
  Downloaded rand v0.4.6
  Downloaded winapi v0.3.9
  Downloaded 4 crates (1.8 MB) in 3.41s (largest was `winapi` at 1.2
MB) Compiling winapi v0.3.9 Compiling libc v0.2.81 Compiling rand v0.4.6 Compiling rand v0.3.23 Compiling game v0.1.0 (E:\DiskZ\rust\0003-game) Finished dev [unoptimized + debuginfo] target(s) in 1m 57s

編譯過程中自動生成的 Cargo.lock 檔案,記錄了相關資源資訊,保證我們以後可以重新構建。

# This file is automatically @generated by Cargo.
# It is not intended for manual editing. [[package]] name = "fuchsia-cprng" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" [[package]] name = "game" version = "0.1.0" dependencies = [ "rand 0.3.23", ] [[package]] name = "libc" version = "0.2.81" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb" [[package]] name = "rand" version = "0.3.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c" dependencies = [ "libc", "rand 0.4.6", ] [[package]] name = "rand" version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" dependencies = [ "fuchsia-cprng", "libc", "rand_core 0.3.1", "rdrand", "winapi", ] [[package]] name = "rand_core" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" dependencies = [ "rand_core 0.4.2", ] [[package]] name = "rand_core" version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" [[package]] name = "rdrand" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" dependencies = [ "rand_core 0.3.1", ] [[package]] name = "winapi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ "winapi-i686-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu", ] [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

我看了一下,下載的程式碼包儲存在 .cargo 資料夾下:
在這裡插入圖片描述