use关键字中的有效路径根是什么? [英] What are the valid path roots in the use keyword?

查看:124
本文介绍了use关键字中的有效路径根是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着2018年版模块系统的改进,use关键字的功能已更改. use关键字之后可以使用的有效路径是什么?

With the module system being revamped for the 2018 edition, the functioning of the use keyword has changed. What are the valid paths that can go after the use keyword?

推荐答案

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

Paths in use statements can only start in the following ways:

  • 外部木箱的名称:然后引用该外部木箱
  • crate:指的是您自己的板条箱(顶层)
  • self:指当前模块
  • super:引用父模块
  • 其他名称:在这种情况下,它将查找与当前模块相对的名称​​相对
  • name of an extern crate: then it refers to that extern crate
  • crate: refers to the (top level of your) own crate
  • self: refers to the current module
  • super: refers to the parent module
  • other name: in this case, it looks for that name relative to the current module

一个示例,演示了所有use-路径(游乐场):

An example demonstrating all kinds of use-paths (Playground):

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`)
}

use语句行为随Rust 2018(在Rust≥1.31中可用)发生了变化.阅读本指南有关使用声明及其在Rust 2018中的更改的更多信息.

This use statement behavior changed with Rust 2018 (available in Rust ≥ 1.31)). Read this guide for more information about use statements and how they changed in Rust 2018.

这篇关于use关键字中的有效路径根是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆