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

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

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

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

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

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

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

手动构建和运行

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

fn main() {
    println!("Hello rust world!");
}

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

调用 rustc 编译文件 hello.rs

$ rustc hello.rs

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

$ 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 看看输出

$ ./hello
Hello rust world!

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

创建一个 package

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

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

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

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

$ cd hello_rust/
$ tree .
.
├── Cargo.toml
└── src
    └── main.rs

1 directory, 2 files

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

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

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

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

回过头来再看 Cargo.toml

$ 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 包,可以如下填写

...
[dependencies]
rand = "0.8.5"

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

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

$ 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)

未完待续…

相关推荐

  1. 初识Cargo-Rust的包管理

    2023-12-15 10:00:01       59 阅读
  2. RustCargo介绍

    2023-12-15 10:00:01       27 阅读
  3. 解决Rust Cargo报错

    2023-12-15 10:00:01       29 阅读
  4. 学习 Rust 的第二天:Cargo管理器的使用

    2023-12-15 10:00:01       31 阅读
  5. rustcargo install cargo-binstall 是什么

    2023-12-15 10:00:01       25 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-15 10:00:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-15 10:00:01       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-15 10:00:01       82 阅读
  4. Python语言-面向对象

    2023-12-15 10:00:01       91 阅读

热门阅读

  1. SpringBoot 上传下载文件

    2023-12-15 10:00:01       60 阅读
  2. vue和jQuery有什么区别

    2023-12-15 10:00:01       56 阅读
  3. python的四大开发包

    2023-12-15 10:00:01       40 阅读
  4. React - 实现一个支持TypeScript类型推导的 useRequest

    2023-12-15 10:00:01       65 阅读
  5. Node.js管理工具npm简单介绍

    2023-12-15 10:00:01       48 阅读
  6. el-select回显

    2023-12-15 10:00:01       66 阅读
  7. vue el-dialog封装成子组件

    2023-12-15 10:00:01       67 阅读
  8. Ubuntu安装MySQL未设置密码/修改密码/删除密码

    2023-12-15 10:00:01       57 阅读
  9. 安卓ViewPager最简单使用(另一种实现)

    2023-12-15 10:00:01       64 阅读
  10. 【09】ES6:Set 和 Map 数据结构

    2023-12-15 10:00:01       48 阅读