我想从另一个模块中调用某个模块的函数(为了代码因素、组织等方面)。以下是我的箱子结构:
➜ mod_test git:(master) ✗ tree
.
├── Cargo.lock
├── Cargo.toml
└── src
├── bin
│ └── one.rs
├── lib
│ └── two.rs
└── main.rs
3 directories, 5 files
在main
函数中,我声明:
pub mod bin {
pub mod one;
}
pub mod lib {
pub mod two;
}
所有这些文件只包含一个简单的pub fn main() {println!("hello");}
。
此时一切正常。
现在,是否可能从bin / one.rs
调用lib :: two :: main
?
bin / one.rs
中添加的use crate :: lib :: two;
,use super :: lib :: two;
和use self :: super :: lib :: two;
都不起作用。
编辑:我在Linux 5.3.0-45-generic上安装了
rustc 1.42.0(b8cedc004 2020-03-09)
,如果有必要。编辑2:每当使用
super
关键字时,我都会从rustc
获得这个神秘的错误:
error[E0433]: failed to resolve: there are too many leading `super` keywords
我找不到任何关于这个问题的故障排除方法。
编辑3:在
src
中添加一个lib.rs
文件,声明lib
模块结构,并在one.rs
中编写use mod_test::lib::two;
可以解决问题,但是:1)这违反了不在我的包中产生“愚蠢的模块声明文件”的想法。
2)我必须在两个不同的地方(
main.rs
和lib.rs
中)完全复制相同的信息。3)使用
use mod_test::lib::two;
是唯一有效的语法,使用crate
或super
关键字仍然会导致晦涩的编译器错误。