rust - 带有通配符使用声明的 Rust 子模块行为
问题描述
我试图了解使用通配符路径时(子)模块导入是如何工作的。我能想到的最简单的演示如下,其中两个模块,或者可能是两个 crate,共享相同的模块结构。
pub mod primary {
pub mod a {
pub mod b {
pub struct A(pub i32);
}
}
}
pub mod import {
pub use crate::primary::*;
// Compiles and executes fine with this commented out, but fails with
// "error[E0433]: failed to resolve: could not find `b` in `a`"
// otherwise. The error refers to the usage in the assert_eq macro
// pub mod a {}
}
fn main() {
assert_eq!(import::a::b::A(42).0, 42);
}
我的一般想法是,由于第一种情况,在pub mod a {}
注释掉的情况下,通配符应该将通配符拾取的所有子模块扩展为它正在扩展的路径中的子模块。不是这样吗?如果是这样,思考的适当方式是什么?
使用声明对此没有太多详细信息。
解决方案
use
使用 a*
导入所有名称,除了那些与当前模块中已经存在的名称冲突的名称。
比较:
pub mod primary {
pub fn f() {
println!("p::f");
}
}
pub mod import {
pub use crate::primary::*;
}
fn main() {
import::f();
}
打印p::f
到_
pub mod primary {
pub fn f() {
println!("p::f");
}
}
pub mod import {
pub use crate::primary::*;
pub fn f() {
println!("import::f");
}
}
fn main() {
import::f();
}
这对于函数和常量来说似乎很明显(否则它会变得*
非常有限,使得上游库不可能在不冒破坏下游用户的情况下添加任何项目),但对于模块来说似乎更加混乱。您必须记住,您不能多次定义一个模块(即“重新打开”一个模块)。以下是非法的:
pub mod primary {
pub mod a {}
pub mod a {}
}
并失败了
error[E0428]: the name `a` is defined multiple times
--> src/lib.rs:3:5
|
2 | pub mod a {}
| --------- previous definition of the module `a` here
3 | pub mod a {}
| ^^^^^^^^^ `a` redefined here
|
= note: `a` must be defined only once in the type namespace of this module
您可以通过再添加一个级别来解决这种特殊情况:
pub mod primary {
pub mod a {
pub mod b {
pub struct A(pub i32);
}
}
}
pub mod import {
pub mod a {
pub use crate::primary::a::*;
}
}
fn main() {
assert_eq!(import::a::b::A(42).0, 42);
}
推荐阅读
- asynchronous - UWP:抛出/引发不会导致应用程序崩溃,异常被忽略
- javascript - 警告:函数作为反应子问题无效......是因为我的容器吗?
- vim - 当我退出插入模式时,如何防止 Vim 复制我的文本?
- excel - 如何将多行合并为一行
- javascript - 请帮助我使用 javascript 构建 Hard Drop 功能(俄罗斯方块)
- java - 如何在 Redis 和 MongoDB 之间同步操作
- r - 识别每一行的字符并在列中返回它们
- list - 使用 Python 连接列表
- angular - Angular 未使用 Dev Extreme 数据网格列和视图进行更新
- java - 多线程终止线程并允许访问其他线程