文章目录
  1. 1. cargo工具的基本使用
  2. 2. cargo-generate
  3. 3. cargo-binstall
  4. 4. cargo中的子命令

Cargo是Rust的包管理工具, 通过rustup脚本来安装rust工具链时会自动安装cargo命令, 可用来创建Rust项目、安装与管理依赖、构建项目与发布包。

cargo工具的基本使用

创建项目

  • cargo new helloworld 新建一个生成可执行文件的项目,等同于cargo new helloworld --bin, 项目文件夹helloworld下将生成文件Cargo.tomlsrc/main.rs两个文件。Cargo.toml是manifest文件, 项目配置的元数据,保存项目信息与依赖的crate
  • cargo new mylib --lib新建生成lib库的项目,在项目文件夹mylib下 创建Cargo.tomlsrc/lib.rs

依赖管理

crates.io是rust社区的包注册仓库,在项目中需要将依赖包的版本添加在Cargo.toml文件中。 这里在前面生成的helloworld项目中添加一依赖ferris-says,打印使用ascii码生成的图形。

  • cargo add ferris-says 使用命令添加是一种方式;移除依赖使用命令cargo remove ferris-says
  • 也可以手动编辑Cargo.toml文件,将依赖库添加在[dependencies]段下
1
2
3
4
5
6
$ cargo add ferris-says
Updating crates.io index
Adding ferris-says v0.3.1 to dependencies.
Features:
- clippy
Updating crates.io index

Cargo.toml文件的内容如下:

1
2
3
4
5
6
7
[package]
name = "hello"
version = "0.1.0"
edition = "2021"
[dependencies]
ferris-says = "0.3.1"

cargo tree 命令可显示依赖关系图

1
2
3
4
5
6
7
8
$ cargo tree
helloworld v0.1.0 (/home/vika/develop/helloworld)
ferris-says v0.3.1
smallvec v1.13.1
textwrap v0.13.4
smawk v0.3.2
unicode-width v0.1.11
unicode-width v0.1.11

编辑src/main.rs文件,引用ferris-says中的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
use ferris_says::say;
use std::io::{ stdout, BufWriter };
fn main() {
//println!("Hello, world!");
let out = "Hello World!";
let width = 24;
let mut writer = BufWriter::new(stdout());
say(out, width, &mut writer).unwrap();
}

构建项目与执行

接下来执行构建命令,生成可执行文件;依赖的包将从crates.io下载到本地, 缓存目录是 $HOME/.cargo/registry

1
2
3
4
5
6
7
8
9
$ cargo build
Downloaded ansi_term v0.12.1
Downloaded atty v0.2.14
Downloaded version_check v0.9.4
...
...
Compiling ferris-says v0.3.1
Compiling helloworld v0.1.0 (/home/vika/develop/helloworld)
Finished dev [unoptimized + debuginfo] target(s) in 1.25s

cargo run命令用于运行构建出的程序

1
2
3
4
5
6
7
8
9
10
11
12
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `/home/vika/develop/helloworld/target/debug/helloworld`
______________
< Hello World! >
--------------
\
\
_~^~^~_
\) / o o \ (/
'_ - _'
/ '-----' \

参考:

  1. The Cargo Book https://doc.rust-lang.org/cargo/index.html
  2. crates.io包注册仓库 https://crates.io/

cargo-generate

cargo-generate 是一个创建rust项目的工具,以git仓库为模板生成新项目的文件。需要先安装后使用。

1
2
3
4
# 安装
cargo install cargo-generate
# 创建项目
cargo generate --git https://github.com/rustwasm/wasm-pack-template
  • cargo-generate 命令的基本格式为cargo generate username-on-github/mytemplate, 默认是从github上的仓库作为模板;也可以在仓库名前添加前缀gh:指明git平台cargo generate gh:username-on-github/mytemplate; 支持的前缀有gh:github.com, gl:gitlab.com, bb:bitbucket.org, sr:git.sr.ht。
  • 或者使用参数--git 直接指定git仓库的全路径,如cargo generate --git https://github.com/username-on-github/mytemplate.git
  • 使用本地仓库作为模板时使用--path参数, cargo generate --path /path/to/template
1
2
3
4
5
cargo generate username-on-github/mytemplate
# is the same as
cargo generate gh:username-on-github/mytemplate
# is the same as
cargo generate --git https://github.com/username-on-github/mytemplate.git

参考:

cargo-binstall

cargo-binstall可以直接从二进制文件来安装Rust程序, 用于简化cargo install直接从源码编译的安装方式。Binstall首先会从crates.io获取包的基本信息,然后从关联的repository仓库查找发布的版本包,未找到时会查找三方托管库quickinstall,仍然未找到时就退回cargo install从源码安装。

1
2
3
4
# 安装binstall
cargo install cargo-binstall
# 使用binstall安装trunk工具
cargo binstall trunk

参考:

前面的例子可以发现,安装的应用是cargo-generate, 在使用是cargo generate而不是直接使用命令cargo-generate,为什么呢? 这里就涉及cargo子命令的调用方式。

Cargo被设计成可以扩展新的子命令,不用修改cargo程序本身。当以cargo (?<command>[^ ]+)形式调用时,会转变成对外部程序cargo-${command}的调用, 当然该外部程序需要在$PATH路径上。 调用子命令时,传给外部程序的第一个参数是程序文件名,第二个参数是子命令名${command}, 命令行中其它参数会原样作为附加参数传给子命令。

执行cargo generate实际上也是转成调用cargo-generate, 第二个参数是generate

cargo也可以打印子命令的帮助信息, cargo help ${command}会转成调用cargo-${command} ${command} --help

参考:

文章目录
  1. 1. cargo工具的基本使用
  2. 2. cargo-generate
  3. 3. cargo-binstall
  4. 4. cargo中的子命令