use 关键字中的有效路径根是什么?

2024-03-08

随着2018年版模块系统的改进,use关键字已更改。之后可以走哪些有效路径use关键词?


路径位于use语句只能以下列方式开始:

  • 外部板条箱的名称:那么它指的是外部板条箱
  • crate: 指的是(你的)自己的箱子的顶层
  • self: 指当前模块
  • super: 指父模块
  • 其他名字:在这种情况下,它会查找该名称relative到当前模块

一个例子展示了各种use-paths ():

pub const NAME: &str = "peter";
pub const WEIGHT: f32 = 3.1;

mod inner {
    mod child {
        pub const HEIGHT: f32 = 0.3;
        pub const AGE: u32 = 3;
    }

    // Different kinds of uses
    use base64::encode;   // Extern crate `base64`
    use crate::NAME;      // Own crate (top level module)
    use self::child::AGE; // Current module
    use super::WEIGHT;    // Parent module (in this case, this is the same 
                          // as `crate`)
    use child::HEIGHT;    // If the first name is not `crate`, `self` or 
                          // `super` and it's not the name of an extern 
                          // crate, it is a path relative to the current
                          // module (as if it started with `self`)
}

This useRust 2018 中的语句行为发生了变化(Rust ≥ 1.31 中可用))。读本指南 https://doc.rust-lang.org/edition-guide/rust-2018/module-system/path-clarity.html有关 use 语句及其在 Rust 2018 中如何更改的更多信息。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

use 关键字中的有效路径根是什么? 的相关文章

随机推荐