欢迎来到 AI 中文社区(简称 AI 中文社),这里是学习交流 AI 人工智能技术的中文社区。 为了更好的体验,本站推荐使用 Chrome 浏览器。
全部教程·
Rust语言·
通过例子学Rust
[目录]
·
10.5. 文件分层
Rust 简介
1. Hello World
1.1. 注释
1.2. 格式化输出
1.2.1 调试(debug)
1.2.2 显示(display)
1.2.3 测试实例:List
1.2.4 格式化
2. 原生类型
2.1. 字面量和运算符
2.2. 元组
2.3. 数组和切片
3. 自定义类型
3.1. 结构体
3.2. 枚举
3.2.1 使用 use
3.2.2 C 风格用法
3.2.3 测试实例:链表
3.3. 常量
4. 变量绑定
4.1. 可变变量
4.2. 作用域和遮蔽
4.3. 变量先声明
4.4. 冻结
5. 类型系统
5.1. 类型转换
5.2. 字面量
5.3. 类型推断
5.4. 别名
6. 类型转换
6.1. From 和 Into
6.2. TryFrom 和 TryInto
6.3. ToString 和 FromStr
7. 表达式
8. 流程控制
8.1. 条件语句 if/else
8.2. 循环语句 loop
8.2.1. 嵌套循环和标签
8.2.2 loop 循环返回
8.3. while 循环
8.4. for 循环
8.5. match 匹配
8.5.1 match 解构元组
8.5.2 match 解构枚举
8.5.3 match 解构指针
8.5.4 match 解构结构体
8.5.5 卫语句
8.5.6 绑定
8.5.7 if let 语句
8.5.8 while let 语句
9. Rust 函数
9.1 Rust 方法
9.2 Rust 闭包
9.2.1 捕获变量
9.2.2 作为输入参数
9.2.3 类型匿名
9.2.4 输入函数
9.2.5 作为输出参数
9.2.6 Iterator::any
9.2.7 Iterator::find
9.3. 高阶函数
9.4. 发散函数
10. 模块
10.1. 可见性
10.2. 结构体的可见性
10.3. use 声明
10.4. super 和 self
10.5. 文件分层
11. crate
11.1. 库
11.2. 使用库
12. cargo
12.1. 依赖
12.2. 约定规范
12.3. 测试
12.4. 构建脚本
13. 属性
13.1 死代码
13.2 crate
13.3 cfg
13.4 自定义条件
14. 泛型
14.1. 函数
14.2. 实现
14.3. trait
14.4. 约束
14.4.1 空约束
14.5. 多重约束
14.6. where 子句
14.7. newtype 惯用法
14.8. 关联项
14.8.1 存在问题
14.9. 虚类型参数
14.9.1 单位检查
Rust 简介
1. Hello World
1.1. 注释
1.2. 格式化输出
1.2.1 调试(debug)
1.2.2 显示(display)
1.2.3 测试实例:List
1.2.4 格式化
2. 原生类型
2.1. 字面量和运算符
2.2. 元组
2.3. 数组和切片
3. 自定义类型
3.1. 结构体
3.2. 枚举
3.2.1 使用 use
3.2.2 C 风格用法
3.2.3 测试实例:链表
3.3. 常量
4. 变量绑定
4.1. 可变变量
4.2. 作用域和遮蔽
4.3. 变量先声明
4.4. 冻结
5. 类型系统
5.1. 类型转换
5.2. 字面量
5.3. 类型推断
5.4. 别名
6. 类型转换
6.1. From 和 Into
6.2. TryFrom 和 TryInto
6.3. ToString 和 FromStr
7. 表达式
8. 流程控制
8.1. 条件语句 if/else
8.2. 循环语句 loop
8.2.1. 嵌套循环和标签
8.2.2 loop 循环返回
8.3. while 循环
8.4. for 循环
8.5. match 匹配
8.5.1 match 解构元组
8.5.2 match 解构枚举
8.5.3 match 解构指针
8.5.4 match 解构结构体
8.5.5 卫语句
8.5.6 绑定
8.5.7 if let 语句
8.5.8 while let 语句
9. Rust 函数
9.1 Rust 方法
9.2 Rust 闭包
9.2.1 捕获变量
9.2.2 作为输入参数
9.2.3 类型匿名
9.2.4 输入函数
9.2.5 作为输出参数
9.2.6 Iterator::any
9.2.7 Iterator::find
9.3. 高阶函数
9.4. 发散函数
10. 模块
10.1. 可见性
10.2. 结构体的可见性
10.3. use 声明
10.4. super 和 self
10.5. 文件分层
11. crate
11.1. 库
11.2. 使用库
12. cargo
12.1. 依赖
12.2. 约定规范
12.3. 测试
12.4. 构建脚本
13. 属性
13.1 死代码
13.2 crate
13.3 cfg
13.4 自定义条件
14. 泛型
14.1. 函数
14.2. 实现
14.3. trait
14.4. 约束
14.4.1 空约束
14.5. 多重约束
14.6. where 子句
14.7. newtype 惯用法
14.8. 关联项
14.8.1 存在问题
14.9. 虚类型参数
14.9.1 单位检查
Rust 文件分层
模块可以分配到文件/目录的层次结构中。让我们将《可见性》一节中 的 例子 的代码拆分到多个文件中:
$ tree . . |-- my | |-- inaccessible.rs | |-- mod.rs | `-- nested.rs `-- split.rs
split.rs 的内容:
// 此声明将会查找名为 `my.rs` 或 `my/mod.rs` 的文件,并将该文件的内容放到
// 此作用域中一个名为 `my` 的模块里面。
mod my;
fn function() {
println!("called `function()`");
}
fn main() {
my::function();
function();
my::indirect_access();
my::nested::function();
}
my/mod.rs 的内容:
// 类似地,`mod inaccessible` 和 `mod nested` 将找到 `nested.rs` 和
// `inaccessible.rs` 文件,并在它们放到各自的模块中。
mod inaccessible;
pub mod nested;
pub fn function() {
println!("called `my::function()`");
}
fn private_function() {
println!("called `my::private_function()`");
}
pub fn indirect_access() {
print!("called `my::indirect_access()`, that\n> ");
private_function();
}
my/nested.rs 的内容:
pub fn function() {
println!("called `my::nested::function()`");
}
#[allow(dead_code)]
fn private_function() {
println!("called `my::nested::private_function()`");
}
my/inaccessible.rs 的内容:
#[allow(dead_code)]
pub fn public_function() {
println!("called `my::inaccessible::public_function()`");
}
我们看到代码仍然正常运行,就和前面的一样:
$ rustc split.rs && ./split called `my::function()` called `function()` called `my::indirect_access()`, that > called `my::private_function()` called `my::nested::function()`
下一章:Rust crate
crate(中文有 “包,包装箱” 之意)是 Rust 的编译单元。当调用 rustc some_file.rs时,some_file.rs 被当作 crate 文件。如果 some_file.rs 里面含有 mod声 ...
AI 中文社