# 模块

前言

在 Rust 中可以通过模块的机制来实现最外层的封装,并且每一个 Rust 文件都可以看作一个模块,模块内的元素可以通过 mod 关键字对外明示

# 1.定义模块

mod nation {
    pub mod government {
        pub fn govern() {}
    }
}
1
2
3
4
5

# 2.使用模块

# 2.1 公共模块

通过关键字use引入公共模块,然后使用

use crate::nation::government::govern;

fn main() {
    govern();
}
1
2
3
4
5

# 2.2 项目模块

通过mod 引入项目中的模块,文件名称为模块名称

mod caret::get_min

fn main(){
  get_min()
}
1
2
3
4
5