【Rust】007-包管理与模块管理
文章目录
- 【Rust】007-包管理与模块管理
- 一、包管理器:Cargo
- 1、简介
- Cargo 官方文档
- 仓库
- 2、项目初始化
- 3、写一个小程序
- 任务目标
- 寻找合适的库
- 添加库到我们的项目中
- 代码实现
- `cargo run`运行
- 二、模块管理
- 1、概述
- 2、文件作为模块
- 第一步:创建文件 `apple.rs`
- 第二步:创建文件 `pear.rs`
- 第三步:在 `main.rs` 引入
- 第四步:调用模块内的函数
- 另一种写法
- 3、文件夹作为模块
- 概述
- 文件结构
- 相关文件
- 在 `main.rs` 中引入并调用函数
- 4、文件内新建子模块
- 子模块代码示例
- `main.rs` 中调用
- 5、一个文件下定义多个模块
- 文件结构
- `fruit.rs` 定义
- `main.rs` 调用
- 6、子模块新版写法
- 说明
- 文件结构
- `fruit.rs` 写法
- `main.rs` 调用
一、包管理器:Cargo
1、简介
Cargo 是Rust 语言的包管理器。Cargo 可以:
- 下载你的 Rust 包的依赖项;
- 编译你的包;
- 创建可分发的包,并将它们上传到crates.io(即Rust社区的包注册表)。
Cargo 官方文档
https://doc.rust-lang.org/cargo/
仓库
2、项目初始化
首先我们新建一个文件夹my_project
,然后为这个文件夹初始化一个 Rust 项目吧!
执行完上述三个命令之后,Cargo 会为my_project
文件夹初始化一个 bin 类型的 Rust 项目(即默认编译目标为一个可执行的二进制文件)。我们可以看一下my_project
文件夹里的内容:
其中src/main.rs
中的main
函数为整个程序的入口。Cargo.toml
为这个项目的配置文件,可以配置包括依赖的包,编译选项等,但因为这个项目是一个新的项目,目前除了基本信息外没有其他内容。
我们用cargo run
运行一下这个项目,成功输出 Hello, world!
如果想要初始化一个库类型的项目,供其他使用方使用(例如发布到 crates.io 上),在初始化的时候需要运行
它会在src
文件夹下新建一个lib.rs
的文件作为整个库的入口。
3、写一个小程序
任务目标
我们随机从 0,1 中取一个数,如果是 0,就输出 Hello,如果是 1 就输出 World。
寻找合适的库
我们首先去https://crates.io/
上找一下有没有随机相关的库可以用。搜索一下就可以发现,有一个使用量非常多的库 rand。
添加库到我们的项目中
第一种方式:
第二种方式:直接编辑Cargo.toml
,在[dependencies]
这一栏下增加:
代码实现
cargo run
运行
二、模块管理
1、概述
当项目非常大的时候,我们需要一种将项目代码根据不同功能划分的方法,且各个功能之间的访问是可以自主控制的。这个方法在 Rust 中就是模块。
通过 Rust 的模块管理,我们可以做到:
- 组织项目代码;
- 可见性控制。
2、文件作为模块
第一步:创建文件 apple.rs
第二步:创建文件 pear.rs
当前目录结构:
第三步:在 main.rs
引入
第四步:调用模块内的函数
另一种写法
3、文件夹作为模块
概述
如果把所有模块都平铺到src/
文件夹下的话,项目一大文件就会非常多。所以我们需要一个通过文件夹组织模块的方法。如果要将文件夹作为模块,在文件夹下一定要有一个mod.rs
的文件。这个文件类似src/main.rs
或者src/lib.rs
,该文件(mod.rs
)控制该文件夹下所有其他文件模块的引入。
文件结构
相关文件
在 main.rs
中引入并调用函数
4、文件内新建子模块
子模块代码示例
在文件内如果想新建模块的话,可以用mod
关键字,且这个是可以嵌套的
main.rs
中调用
5、一个文件下定义多个模块
文件结构
fruit.rs
定义
main.rs
调用
6、子模块新版写法
说明
新版本的改进:在较新的 Rust 版本中,你可以将 fruit.rs
模块的子模块放在一个同名的目录中,即 fruit/
目录。这种方式让代码结构更清晰,尤其是当模块变得复杂时。
例如,你有一个 fruit.rs
文件,它是一个模块。你可以在项目的同一层级下创建一个名为 fruit
的目录,然后在这个目录中放置该模块的子模块文件。