如何用 Cargo 管理 Rust 工程系列 甲

以下内容为本人的学习笔记,如需要转载,请声明原文链接微信公众号「ENG八戒」mp.weixin.qq.com/s/ceMTUzRjD...

这几年 Rust 可谓是炙手可热的新兴编程语言了,而且被投票为最受程序员喜爱的语言。它很现代,专门为了性能、可靠和生产力而设计,说人话就是非常快,不容易崩溃,开发效率高。

那么,如此热门的计算机语言,它如何去构建软件和管理构建工程呢?

Rust 语言的开发团队提供了标准方案,利用 rust 自带的一个包 Cargo 帮助开发人员灵活高效地构建代码。Cargo 提供下载各式各样的库或者工程依赖项,发布自己的包和上传到 crates.io 共享给所有开发者等。

下面让我们一起来看看什么是 cargo,它可以如何帮助我们开发者构建自己的 rust 工程。关于怎么安装 rust 和 cargo 相关可以查看八戒的另一篇文章《简明快速配置 Rust 工具链》。

本文以下演示都是基于 ubuntu 18.04.6。

手动构建和运行

先来看看直接使用 rust 的编译器 rustc 编译 hello world 示例。准备一个 rust 源码文件,输入以下内容

arduino 复制代码
fn main() {
    println!("Hello rust world!");
}

rust 源码文件的后缀为 (.rs),所以上面的文件保存为 hello.rs

调用 rustc 编译文件 hello.rs

ruby 复制代码
$ rustc hello.rs

编译没有错误返回意味着成功结束,这时生成了可执行文件 hello,与输入源文件同名

sql 复制代码
$ ll
total 4544
drwxrwxrwx 1 user user     512 Nov 14 00:55 ./
drwxrwxrwx 1 user user     512 Nov 14 00:54 ../
-rwxrwxrwx 1 user user 4652168 Nov 14 00:55 hello
-rwxrwxrwx 1 user user      50 Nov 14 00:54 hello.rs

再手动执行文件 hello 看看输出

shell 复制代码
$ ./hello
Hello rust world!

这样的构建过程,一切挺顺利的,尤其是我们突然灵光一闪,脑袋有个想法需要快速验证时,这样也不错。但是这只是编译了一个源码文件,要是我们的开发工程异常庞大,源码文件去到上百上千个文件时,再使用编译器 rustc 手动编译真的太费劲啦。就好像现在的大型 C/C++ 工程基本都用 cmake 管理一样,咱的 rust 工程有 cargo。

创建一个 package

Cargo 是 rust 团队的指定构建系统和包管理器,可以利用它快速创建一个空的 package 工程,执行构建的时候 cargo 会根据配置文件内容自动下载依赖项等。相信大伙在碰到 rust 的另一个概念 crate 是会感到很困惑,其实一般情况下 package 都可以被当做是 crate,就是说可以互换。

下面来看看怎么创建新 package 工程

go 复制代码
$ cargo new hello_rust
     Created binary (application) `hello_rust` package

上面示例使用了 cargo new 指令创建一个名字是 hello_rust 的新 package 工程,然后用 tree 指令看看都自动创建了哪些文件和路径

shell 复制代码
$ cd hello_rust/
$ tree .
.
├── Cargo.toml
└── src
    └── main.rs

1 directory, 2 files

可以看到有个 Cargo.toml 文件,它是 cargo 构建 package 工程的配置文件,不过看起来有点陌生,先继续看其它内容。

在 src 文件夹下有个 rust 源码文件 main.rs,打开看看里边的内容

arduino 复制代码
$ cat src/main.rs 
fn main() {
    println!("Hello, world!");
}

原来 main.rs 包含了一个 hello world 的示例代码,真的是一步到位。

回过头来再看 Cargo.toml

ini 复制代码
$ cat Cargo.toml 
[package]
name = "hello_rust"
authors = ["ENG八戒"]
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

可以看到文件 Cargo.toml 内包含了多个 section,比如 package、dependencies。

在 package 字段,包含了工程的名字 name,作者 authors,工程版本 version,rust 版本等。格式是键值对的形式,= 后边的值用 [] 界定表示该值类型是列表的形式,内容可以是多个,比如软件工程的作者就可以有多个。

而 dependencies 字段用于填写该工程的依赖项,依赖项就是我们在开发软件时,有很多的内容可以直接借用别人的成果,或者避免浪费自己的时间重复造轮子。而别人的成果通常以包的形式提供,我们只需要在这个配置文件的 dependencies 字段里填写对应的包信息。

假设依赖项中包含产生随机数的 rand 包,可以如下填写

ini 复制代码
...
[dependencies]
rand = "0.8.5"

上面填写的依赖项内容不仅有包名,还有具体的版本。但是,我们怎么知道现时有哪些具体的包和版本呢?

可以用 cargo search 指令,后边加上对应的模糊包名,这样 cargo 就会从 crates.io 查找可用包并打印,你再从中选一个合适的

ini 复制代码
$ cargo search rand
rand = "0.8.5"              # Random number generators and other randomness functionality. 
bevy_rand = "0.4.0"         # A plugin to integrate rand for ECS optimised RNG for the Bevy game engine.
tinyrand = "0.5.0"          # Lightweight RNG specification and several ultrafast implementations in Rust.
random_derive = "0.0.0"     # Procedurally defined macro for automatically deriving rand::Rand for structs and enums
tera-rand = "0.2.0"         # A suite of random data generation functions for the Tera template engine
tera-rand-cli = "0.2.0"     # A CLI tool for generating a feed of random data from a Tera template
faker_rand = "0.1.1"        # Fake data generators for lorem ipsum, names, emails, and more
rand_derive2 = "0.1.21"     # Generate customizable random types with the rand crate
fake-rand-test = "0.0.0"    # Random number generators and other randomness functionality.
ndarray-rand = "0.14.0"     # Constructors for randomized arrays. `rand` integration for `ndarray`.
... and 1219 crates more (use --limit N to see more)

未完待续...

相关推荐
码力斜杠哥6 小时前
Rust初习录(6)Rust的 if 玩法
开发语言·python·rust
Rust研习社7 小时前
Rust 的 move 语义,一次讲透
后端·rust·编程语言
WMYeah11 小时前
【无标题】
前端·rust·抽奖程序·跨平台抽奖程序
楼兰公子1 天前
buildroot 在编译rust时裁剪平台类型数量的方法
开发语言·后端·rust
Rust研习社1 天前
开源项目里的 deny.toml 是什么?
后端·rust·编程语言
铭毅天下1 天前
当搜索引擎遇上 Rust——深度解读下一代实时搜索引擎 INFINI Pizza
开发语言·后端·搜索引擎·rust
咸甜适中2 天前
rust语言学习笔记Trait之Default(默认值)
笔记·学习·rust
容智信息2 天前
AI Agent(智能体)的输出格式应该从 Markdown 转向 HTML吗?
前端·人工智能·rust·编辑器·html·prompt
Rust研习社2 天前
Rust Clippy 实用指南:写出更优雅、安全的 Rust 代码
后端·rust·编程语言