代码点读机 / Rust / super

super Rust · 关键字

指代当前模块的父模块,用来向上走一层引用路径。

怎么用

super::模块名 或 use super::项;,用于访问父模块

小例子

mod outer {
    pub fn hello() -> &'static str { "hi" }
    pub mod inner {
        pub fn call() {
            println!("{}", super::hello());
        }
    }
}
fn main() {
    outer::inner::call();
}

运行结果

hi

提醒:类似文件系统的 ..,表示上一级目录;只在子模块内部有意义

官方文档:https://doc.rust-lang.org/reference/paths.html#r-paths.qualifiers.super

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Rust全部词条