$ curl -sSf https://static.rust-lang.org/rustup.sh | sh
在 Linux 和 macOS 系統(tǒng)上,這可以通過以上方命令完成
| :- | :- |
|---|---|
cargo version | 顯示版本信息以確認 Cargo 已安裝 |
cargo new | 創(chuàng)建一個新項目 |
cargo test | 在項目中運行單元測試 |
cargo check | 快速編譯項目,無需生成二進制文件來檢查錯誤 |
cargo fmt | 自動格式化代碼 |
cargo build | 編譯一個項目 |
cargo run | 一步編譯和運行項目 |
cargo clippy --all-targets -- --D warnings | Linter 檢查錯誤 |
cargo tarpaulin --ignore-tests | 檢查代碼覆蓋率 |
$ touch ~/.cargo/config # 添加配置文件
$ vim ~/.cargo/config # 編輯配置文件
配置文件 config 內(nèi)容
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'tuna' # ?? 如果需要提交包注釋配置源
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
# registry = "git://mirrors.ustc.edu.cn/crates.io-index"
?? 注意切換源需要刪除緩存目錄
$ rm -rf ~/.cargo/.package-cache # ?? 刪除緩存目錄內(nèi)容
$ cargo new hello_world --bin
--bin 正在制作一個二進制程序--lib 正在創(chuàng)建一個庫(lib)得到如下目錄:
$ cd hello_world
$ tree .
.
├── Cargo.toml
└── src
└── main.rs
Cargo.toml 被稱為一個 manifest 元清單,它包含了 Cargo 編譯項目所需的所有元數(shù)據(jù)
[package]
name = "hello_world"
version = "0.1.0"
authors = ["Your Name <you@example.com>"]
edition = "2018"
[dependencies]
入口文件 src/main.rs
fn main() {
println!("Hello, world!");
}
運行編譯生成 hello_world 二進制文件
$ cargo build
# 文件放入 `target/release` 目錄
$ cargo build --release
然后運行它:
$ ./target/debug/hello_world
Hello, world!
也可以直接使用 cargo run,它會自行編譯并運行它:
$ cargo run
Running `target/hello_world`
Hello, world!
# `source` 表下,就是存儲有關(guān)要更換的來源名稱
[source]
# 在`source` 表格之下的,可為一定數(shù)量的有關(guān)來源名稱. 示例下面就,定義了一個新源, 叫 `my-awesome-source`,其內(nèi)容來自本地,`vendor`目錄 ,其相對于包含 `.cargo/config` 文件的目錄
[source.my-awesome-source]
directory = "vendor"
# Git sources 也指定一個 branch/tag/rev
git = "https://example.com/path/to/repo"
# branch = "master"
# tag = "v1.0.1"
# rev = "313f44e8"
# crates.io 默認源 在"crates-io"名稱下,且在這里我們使用 `replace-with` 字段指明 默認源更換成"my-awesome-source"源
[source.crates-io]
replace-with = "my-awesome-source"
# 編譯輸出二進制文件,放入 `target/debug` 目錄
$ cargo build
# 輸出二進制文件,放入 `target/release` 目錄
$ cargo build --release
$ cargo run # 編譯并運行
$ cargo test # 運行你的所有測試
# 指定函數(shù)過濾器
$ cargo test test_foo # 開頭是 test_foo 的函數(shù)都會運行,例如(test_foo_bar)
# 指定特定模塊中的測試函數(shù)(通??梢院唽?cargo test foo::bar::tests::test_foo)
$ cargo test --package rustt --lib -- foo::bar::tests::test_foo --exact --nocapture
# 指定特定測試的模塊(通常可以簡寫 cargo test foo::bar::tests)
$ cargo test --package rustt --lib -- foo::bar::tests --nocapture
[package]
# ...
[lib]
# 生成目標與庫的名稱. 本該默認是包名, 替換所有破折號為 下劃線. (Rust `extern crate` 聲明會參考該名;因此,該值必須是可用的有效Rust標識符)
name = "foo"
# 該字段,指向 crate 的入口(位置), 路徑相對于 `Cargo.toml`.
path = "src/lib.rs"
# 一個給目標啟用單元測試 的 標志. 會被 `cargo test`使用.
test = true
# 一個給目標啟用文檔測試 的 標志. 只與庫相關(guān), 不會影響其他部分。會被 `cargo test`使用.
doctest = true
# 一個給目標啟用基準 的 標志. 會被 `cargo bench`使用.
bench = true
# 一個給目標啟用文檔 的 標志. 會被 `cargo doc`使用.
doc = true
# 若該目標為 編譯器擴展, 那要把該字段設(shè)為 true,以讓 Cargo 正確編譯和,可用于所有依賴項.
plugin = false
# 若該目標為 "macros 1.1" 程序宏, 那要把該字段設(shè)為 true
proc-macro = false
# 若設(shè)為 false, `cargo test` 會為 rustc 省略 `--test` 標志, 這阻止它生成測試工具 這在二進制存在,構(gòu)建管理測試運行器本身的情況下,有用.
harness = true
# 若設(shè)置了,那 目標會使用一個與`[package]`配置不同的版本, 也許是,編譯一個庫 2018年版本或,編譯單元測試的2015年版本. 默認情況下所有目標都使用`[package]`中指定的版本進行編譯。
edition = '2015'
.
├── Cargo.lock
├── Cargo.toml
├── benches # 基準目錄
│ └── large-input.rs
├── examples # 示例
│ └── simple.rs
├── src # 源代碼
│ ├── bin
│ │ └── another_executable.rs
│ ├── lib.rs # 默認庫
│ └── main.rs # 入口文件
└── tests # 集成測試
└── some-integration-tests.rs
# 每個源都有自己的表格,名稱即是表名
[source.the-source-name]
# 命令 `the-source-name` 會被 `another-source`取代
replace-with = "another-source"
# 有幾種可用的源定義(接下來有所描述)
registry = "https://example.com/path/to/index"
local-registry = "path/to/registry"
directory = "path/to/vendor"
更換源的配置通過完成 .cargo/config,上面是全套可用字段
創(chuàng)建一個新的 cargo 包
$ cargo init [options] [path]
$ cargo new [options] [path]
--bin 創(chuàng)建具有二進制目標 (src/main.rs) 的包(默認行為)--lib 使用庫目標 (src/lib.rs) 創(chuàng)建一個包--edition edition 指定要使用的 Rust 版本。默認值為 2021??赡艿闹担?015、2018、2021--name name 設(shè)置包名。默認為目錄名稱--vcs vcs 為給定的版本控制系統(tǒng)(git、hg、pijul 或fossil)初始化一個新的 VCS 存儲庫,或者根本不初始化任何版本控制(無)--registry registry 限制僅發(fā)布到該注冊表$ cargo new foo
# 從 crates.io 安裝或升級軟件包:
$ cargo install ripgrep
# 在當前目錄安裝或重新安裝包:
$ cargo install --path .
# 查看已安裝包的列表:
$ cargo install --list
--vers version--version version 指定要安裝的版本--git url 用于安裝指定 crate 的 Git URL--branch branch 從 git 安裝時要使用的分支--tag tag 從 git 安裝時使用的標記--rev sha 從 git 安裝時使用的特定提交--path path 要安裝的本地 crate 的文件系統(tǒng)路徑--list 列出所有已安裝的軟件包及其版本-f, --force 強制覆蓋現(xiàn)有的 crate 或二進制文件$ cargo search [options] [query...]
--limit limit 限制結(jié)果數(shù)量(默認值:10,最大值:100)--index index 要使用的注冊表索引的 URL--registry registry 要使用的注冊表的名稱$ cargo search serde
$ cargo uninstall [options] [spec...]
-p, --package spec... 要卸載的軟件包--bin name... 僅卸載二進制名稱--root dir 從中卸載軟件包的目錄$ cargo uninstall ripgrep
$ cargo login [options] [token]
| :- | :- |
|---|---|
--registry | 要使用的注冊表的名稱 |
| :- | :- |
|---|---|
-v, --verbose | 啟用更加詳細的輸出 |
-q, --quiet | 不輸出Cargo的日志信息 |
--color when | 輸出內(nèi)容的顏色 auto, always, never |
# 列出包的所有者:
$ cargo owner --list foo
# 邀請所有者加入包:
$ cargo owner --add username foo
# 從包中刪除所有者:
$ cargo owner --remove username foo
| :- | :- |
|---|---|
--token token | 身份驗證時使用的 API 令牌 |
--index index | 要使用的注冊表索引的 URL |
選擇包
-p spec..., --package spec... Package 指定包--workspace Package 工作區(qū)中的全體成員--exclude SPEC... 排除指定包編譯選項
--target triple 為指定架構(gòu)執(zhí)行 Package--target-dir directory 用于存放生成的工件以及中間文件的目錄特性選擇
--features features 傳遞以空格或者逗號分隔的列表,其中給出要啟用的特性--all-features 為給定的包啟用全部可用特性--no-default-features 不啟用給定包的default特性清單選項
--manifest-path path 用于指定 Cargo.toml 文件的路徑--frozen, --locked 這兩個選項用于保證Cargo.lock文件是最新的--offline 禁止Cargo訪問網(wǎng)絡(luò)混雜選項
-j N, --jobs N 要并行運行的作業(yè)數(shù)量將本地包打包為可分發(fā)的壓縮文件
$ cargo package [options]
-l, --list 輸出包中包含的文件(不實際進行打包)。--no-verify 構(gòu)建包時不進行校驗。--no-metadata 忽略 缺少可讀的元信息(如描述信息或采用的授權(quán)協(xié)議) 時產(chǎn)生的警告。--allow-dirty 允許打包 在版本控制系統(tǒng)中仍有未提交內(nèi)容 的包。$ cargo publish [options]
發(fā)布選項
--dry-run 在不上傳的情況下執(zhí)行所有檢查--token token 身份驗證時使用的 API 令牌--no-verify 不要通過構(gòu)建內(nèi)容來驗證內(nèi)容--allow-dirty 允許打包具有未提交的 VCS 更改的工作目錄--index index 要使用的注冊表索引的 URL--registry registry 要發(fā)布到的注冊表的名稱打包選擇
-p spec, --package spec 要發(fā)布的包從服務(wù)器的索引中刪除以前發(fā)布的 crate 版本
$ cargo yank --vers 1.0.7 foo
--vers version 要 yank 或 un-yank 的版本--undo 撤消 yank,將版本放回索引中--token token 身份驗證時使用的 API 令牌--index index 要使用的注冊表索引的 URL--registry registry 要使用的注冊表名稱感谢您访问我们的网站,您可能还对以下资源感兴趣:
国产成人精品999视频&日本一区二区亚洲人妻精品&久久久精品国产99久久精&99热这里只有成人精品国产&精品国产剧情av一区二区&成人亚洲精品久久久久app&国产精品美女高潮抽搐A片