路径有两种形式:

  • 绝对路径(absolute path):从 crate 根开始,以 crate 名或者字面值 crate 开头
  • 相对路径(relative path):从当前模块开始,以 self、super 或当前模块的标识符开头

绝对路径和相对路径都后跟一个或多个由双冒号(::)分割的标识符。

  1. mod front_of_house {
  2. mod hosting {
  3. fn add_to_waitlist() {}
  4. }
  5. }
  6. pub fn eat_at_restaurant() {
  7. // 绝对路径
  8. crate::front_of_house::hosting::add_to_waitlist();
  9. // 相对路径
  10. front_of_house::hosting::add_to_waitlist();
  11. }

绝对路径:add_to_waitlist 函数与 eat_at_restaurant 被定义在同一 crate 中,这意味着我们可以使用 crate 关键字为起始的绝对路径。通过指定路径 /front_of_house/hosting/add_to_waitlist 来执行 add_to_waitlist 程序。
相对路径:这个路径以 front_of_house 为起始,这个模块在模块树中,与 eat_at_restaurant 定义在同一层级。与之等价的文件系统路径就是 front_of_house/hosting/add_to_waitlist。以名称为起始,意味着该路径是相对路径。
两种路径的选择,取决于项目:取决于你是更倾向于将项的定义代码与使用该项的代码分开来移动,还是一起移动
举一个例子,如果我们要将 front_of_house 模块和 eat_at_restaurant 函数一起移动到一个名为 customer_experience 的模块中,我们需要更新 add_to_waitlist 的绝对路径,但是相对路径还是可用的。然而,如果我们要将 eat_at_restaurant 函数单独移到一个名为 dining 的模块中,还是可以使用原本的绝对路径来调用 add_to_waitlist,但是相对路径必须要更新。我们更倾向于使用绝对路径,因为把代码定义和项调用各自独立地移动是更常见的。
Rust 中默认所有项(函数、方法、结构体、枚举、模块和常量)都是私有的父模块中的项不能使用子模块中的私有项,但是子模块中的项可以使用他们父模块中的项。这是因为子模块封装并隐藏了他们的实现详情,但是子模块可以看到他们定义的上下文。
Rust 这种模块系统设计可以帮助用户知道更改哪些内部代码不会破坏外部代码。

使用 pub 关键字暴露路径

  1. mod front_of_house {
  2. pub mod hosting {
  3. pub fn add_to_waitlist() {}
  4. }
  5. }
  6. pub fn eat_at_restaurant() {
  7. // 绝对路径
  8. crate::front_of_house::hosting::add_to_waitlist();
  9. // 相对路径
  10. front_of_house::hosting::add_to_waitlist();
  11. }

在绝对路径,我们从 crate,也就是 crate 根开始。然后 crate 根中定义了 front_of_house 模块。front_of_house 模块不是公有的,不过因为 eat_at_restaurant 函数与 front_of_house 定义于同一模块中(即,eat_at_restaurant 和 front_of_house 是兄弟),我们可以从 eat_at_restaurant 中引用 front_of_house。接下来是使用 pub 标记的 hosting 模块。我们可以访问 hosting 的父模块,所以可以访问 hosting。最后,add_to_waitlist 函数被标记为 pub ,我们可以访问其父模块,所以这个函数调用是有效的!

使用 super 起始的相对路径

super 类似文件系统中以 ..开头的语法。

  1. fn serve_order() {}
  2. mod back_of_house {
  3. fn fix_incorrect_order() {
  4. cook_order();
  5. super::serve_order();
  6. }
  7. fn cook_order() {}
  8. }

创建公有的结构体和枚举

如果我们在一个结构体定义的前面使用了 pub,这个结构体会变成公有的,但是这个结构体的字段仍然是私有的

  1. mod back_of_house {
  2. pub struct Breakfast {
  3. pub toast: String,
  4. seasonal_fruit: String,
  5. }
  6. impl Breakfast {
  7. pub fn summer(toast: &str) -> Breakfast {
  8. Breakfast {
  9. toast: String::from(toast),
  10. seasonal_fruit: String::from("peaches"),
  11. }
  12. }
  13. }
  14. }
  15. pub fn eat_at_restaurant() {
  16. // 在夏天订购一个黑麦土司作为早餐
  17. let mut meal = back_of_house::Breakfast::summer("Rye");
  18. // 改变注意更换想要面包的类型
  19. meal.toast = String::from("Wheat");
  20. println!("I'd like {} toast please", meal.toast);
  21. // 如果取消下一行的注释代码不能编译;
  22. // 不允许查看或修改早餐附带的季节水果
  23. // meal.seasonal_fruit = String::from("blueberries");
  24. }

如果我们将枚举设为公有,则它的所有成员都将变为公有

  1. mod back_of_house {
  2. pub enum Appetizer {
  3. Soup,
  4. Salad,
  5. }
  6. }
  7. pub fn eat_at_restaurant() {
  8. let order1 = back_of_house::Appetizer::Soup;
  9. let order2 = back_of_house::Appetizer::Salad;
  10. }