gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:02:46 25 4
gpt4 key购买 nike

随着 2018 版模块系统的改进,use 关键字的功能发生了变化。 use 关键字后的有效路径是什么?

最佳答案

use 语句中的路径只能以下列方式开始:

  • 外部包装箱的名称:然后它指的是那个外部包装箱
  • crate:指的是(您的)自己的 crate
  • self:指当前模块
  • super:指父模块
  • other name:在本例中,它查找当前模块的相对名称

演示各种使用路径的示例(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 中可用)。阅读this guide有关 use 语句及其在 Rust 2018 中如何更改的更多信息。

关于module - use 关键字中的有效路径根是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54289071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com