22_路径

1. 概述

在rust里,如果我们想要找到某个模块,我们必须知道其路径,rust的路径有两种形式。

绝对路径:从crate root开始,使用 crate 名或字面值 crate。相对路径:从当前模块开始,使用self、super,或者当前模块的标识符。不管是绝对路径还是相对路径,只有由一个标识符组成,如果存在多个标识符,标识符之间使用::进行分割。

示例代码如下src/lib.rs

rust 复制代码
mod front_of_house {
    mod hosting {
        fn add_to_waitlist() {}
    }
}

pub fn eat_at_restaurant() {
    // 使用绝对路径调用函数
    crate::front_of_house::hosting::add_to_waitlist();
    // 使用相对路径调用函数
    front_of_house::hosting::add_to_waitlist();
}

以上的代码分别使用绝对路径和相对路径调用函数,front_of_house模块相对eat_at_restaurant函数在同一个级别,所以函数里可以直接调用。不过上面的代码看似没有问题,但当我们编译的时候会报错。我们将在下文解决这个问题。

2. 私有边界(privacy boundary)

模块不仅可以组织代码,还可以定义私有边界。如果想把函数或者struct等设置为私有,就可以将它放到某个模块中。Rust中所有条目(函数、方法、struct、enum、模块、常量)默认是私有的。父级模块无法访问子级模块中的私有条目,在子模块里,可以使用所有祖先模块的条目。

2.1 pub关键字

使用pub关键字可以将某些条目标记为公共的。我们将"一"中的代码改为如下,将不会再出现错误

rust 复制代码
mod front_of_house {
    pub mod hosting {
        pub fn add_to_waitlist() {}
    }
}

pub fn eat_at_restaurant() {
    // 使用绝对路径调用函数
    crate::front_of_house::hosting::add_to_waitlist();
    // 使用相对路径调用函数
    front_of_house::hosting::add_to_waitlist();
}

front_of_house之所以不需要添加pub关键字,因为front_of_houseeat_at_restaurant都在文件根级,可以互相调用。

2.2 super关键字

super关键字用来访问父级模块中的内容,类型文件系统中的..。如下示例代码

rust 复制代码
fn serve_order() {}

mod back_of_house {
    fn fix_incorrect_order() {
        cook_order();
        // 使用相对路径的方式调用
        super::serve_order();
        // 使用绝对路径的方式调用
        crate::serve_order();
    }

    fn cook_order() {}
}

2.3 pub struct

pub 放在struct 之前,代表公共的结构体,struct 里面的字段默认是私有的,想把哪个字段设置为公有,就在对应的字段前面加上pub关键字。如下示例代码

rust 复制代码
mod back_of_house {
    pub struct Breakfast {
        pub toast: String,
        seasonal_fruit: String,
    }

    impl Breakfast {
        pub fn summer(toast: &str) -> Breakfast {
            Breakfast {
                toast: String::from(toast),
                seasonal_fruit: String::from("peachers"),
            }
        }
    }
}

pub fn eat_at_restaurant() {
    let mut meal = back_of_house::Breakfast::summer("Rye");
    meal.toast = String::from("Wheat");
    println!("I'd like {} toast please", meal.toast);
}

2.4 pub enum

pub 放在 enum 前面是将enum声明为公共。枚举中的变体都变成公共的。如下示例代码

rust 复制代码
mod back_of_house {
    pub enum Appetizer {
        Soup,
        Salad,
    }
}
相关推荐
Mr.Rice.Fool5 小时前
rust面试经验1
后端·面试·职场和发展·rust
kyriewen6 小时前
Webpack vs Vite:一个是“老黄牛”,一个是“猎豹”,你选谁?
前端·webpack·vite
打小就很皮...6 小时前
html2canvas + jsPDF 生成 PDF 的踩坑与解决方案总结
前端·pdf
全栈前端老曹6 小时前
【前端地图】多地图平台适配方案——高德、百度、腾讯、Google Maps SDK 差异对比、封装统一地图接口
前端·javascript·百度·dubbo·wgs84·gcj-02·bd09
本地化文档6 小时前
rust-nomicon-l10n
rust·github·gitcode
代码羊羊6 小时前
Rust 格式化输出完全攻略:从入门到精通
开发语言·后端·rust
Rust研习社6 小时前
Rust + PostgreSQL 极简技术栈应用开发
开发语言·数据库·后端·http·postgresql·rust
雾岛听风6916 小时前
JavaScript基础语法速查手册
开发语言·前端·javascript
遇见~未来6 小时前
第三篇_现代布局_从弹性到网格
前端·css3
前端那点事6 小时前
Vue前端SEO优化全攻略(实操落地版,新手也能上手)
前端·vue.js