从 Rust 中的多个目录导入代码
Import code from multiple directories in Rust
抱歉,如果是重复的问题。我在网上/Whosebug 上看了看,但找不到答案。我想从 Rust 中的不同文件夹/文件导入 mod 规则。
我的文件/文件夹结构如下:
src/
test_1/
mod.rs <- inside here I have put: pub mod a, and pub mod b
a.rs
b.rs
tes_2/
mod.rs <- inside here I have put: pub mod a1, and pub mod b1
a1.rs
b1.rs
test_3/
mod.rs<- inside here I have put: pub mod a2, and pub mod b2
a2.rs
b2.rs
我想与 a1.rs
和 a2.rs
共享我在 a.rs
中的代码,或者与 a.rs
共享我在 b2.rs
中的代码。
我尝试在文件中添加 mod 和使用,我也尝试了很多在线示例,但没有任何效果。
你能帮忙吗?提前致谢。
在网上看了之后,经过Lukas的评论,我找到了解决办法。我将尝试在这里总结一下。
我必须在文件夹外创建一个名为 lib.rs 的新文件:
src/
test_1/
mod.rs <- inside here I have put: pub mod a, and pub mod b
a.rs
b.rs
tes_2/
mod.rs <- inside here I have put: pub mod a1, and pub mod b1
a1.rs
b1.rs
test_3/
mod.rs<- inside here I have put: pub mod a2, and pub mod b2
a2.rs
b2.rs
lib.rs
在此文件中,我添加了以下代码:
pub mod test_1
pub mod test_2
pub mod test_3
而且成功了!我现在可以使用 use crate
命令从不同文件导入代码:
示例:
use create::test_1::a1::function_name;
抱歉,如果是重复的问题。我在网上/Whosebug 上看了看,但找不到答案。我想从 Rust 中的不同文件夹/文件导入 mod 规则。
我的文件/文件夹结构如下:
src/
test_1/
mod.rs <- inside here I have put: pub mod a, and pub mod b
a.rs
b.rs
tes_2/
mod.rs <- inside here I have put: pub mod a1, and pub mod b1
a1.rs
b1.rs
test_3/
mod.rs<- inside here I have put: pub mod a2, and pub mod b2
a2.rs
b2.rs
我想与 a1.rs
和 a2.rs
共享我在 a.rs
中的代码,或者与 a.rs
共享我在 b2.rs
中的代码。
我尝试在文件中添加 mod 和使用,我也尝试了很多在线示例,但没有任何效果。
你能帮忙吗?提前致谢。
在网上看了之后,经过Lukas的评论,我找到了解决办法。我将尝试在这里总结一下。
我必须在文件夹外创建一个名为 lib.rs 的新文件:
src/
test_1/
mod.rs <- inside here I have put: pub mod a, and pub mod b
a.rs
b.rs
tes_2/
mod.rs <- inside here I have put: pub mod a1, and pub mod b1
a1.rs
b1.rs
test_3/
mod.rs<- inside here I have put: pub mod a2, and pub mod b2
a2.rs
b2.rs
lib.rs
在此文件中,我添加了以下代码:
pub mod test_1
pub mod test_2
pub mod test_3
而且成功了!我现在可以使用 use crate
命令从不同文件导入代码:
示例:
use create::test_1::a1::function_name;