Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Module use关键字中的有效路径根是什么?_Module_Rust_Rust 2018 - Fatal编程技术网

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

Module use关键字中的有效路径根是什么?,module,rust,rust-2018,Module,Rust,Rust 2018,随着2018年版模块系统的改进,use关键字的功能发生了变化。在使用关键字之后可以使用哪些有效路径?您的路径可以以两种不同的方式开始:绝对路径或相对路径: 您的路径可以以板条箱名称开头,也可以使用板条箱关键字来命名当前板条箱: struct Foo; mod module { use crate::Foo; // Use `Foo` from the current crate. use serde::Serialize; // Use `Serialize` from th

随着2018年版模块系统的改进,
use
关键字的功能发生了变化。在
使用
关键字之后可以使用哪些有效路径?

您的路径可以以两种不同的方式开始:绝对路径或相对路径:

  • 您的路径可以以板条箱名称开头,也可以使用
    板条箱
    关键字来命名当前板条箱:

    struct Foo;
    
    mod module {
        use crate::Foo; // Use `Foo` from the current crate.
        use serde::Serialize; // Use `Serialize` from the serde crate.
    }
    
    fn main() {}
    
  • 否则,根显式地为
    self
    ,这意味着您的路径将与当前模块相关:

    mod module {
        pub struct Foo;
        pub struct Bar;
    }
    
    use module::Foo; // By default, you are in the `self` (current) module.
    use self::module::Bar; // Explicit `self`.
    
    在此上下文中,您可以使用
    super
    访问外部模块:

    struct Foo;
    
    mod module {
        use super::Foo; // Access `Foo` from the outer module.
    }
    

  • use
    语句中的路径只能以以下方式启动:

    • 外部板条箱的名称:那么它指的是该外部板条箱
    • 板条箱
      :指您自己的板条箱(顶层)
    • self
      :指当前模块
    • super
      :指父模块
    • 其他名称:在本例中,它查找与当前模块相关的名称
    演示各种
    使用
    -path()的示例:


    use
    语句行为随Rust 2018(Rust中提供)而改变≥ 1.31)). 阅读了解更多有关Rust 2018中的使用声明及其变化的信息。

    也允许使用前导的
    ,这相当于
    板条箱::
    。有些人认为它很难看,但它在2018版之前的代码中被大量使用,而2018版是在
    crainte::
    被引入的时候。@PeterHall你确定吗?我明白了,它没有编译。我将修改这个问题,以2018年版为目标。啊,我甚至没有意识到2018年版不再适用!
    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`)
    }