从Rust中的多个目录导入代码

gpnt7bae  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(165)

抱歉,如果这是一个重复的问题。我看了online / stackoverflow,我找不到答案。我想从Rust中的不同文件夹/文件导入模块。
我的文件/文件夹的结构如下:

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.rsa2.rs共享我在a.rs中的代码,或者与a.rs共享我在b2.rs中的代码。
我试过添加mod和使用内部的文件,我也尝试了许多例子在线,但没有任何工作。
你能帮忙吗?先谢谢你。

h43kikqp

h43kikqp1#

在网上看过之后,在卢卡斯评论之后,我找到了解决方案。我将试着在这里总结一下。
我必须在文件夹外创建一个名为www.example.com的新文件lib.rs:

src/
  test_1/
     mod.rs <- inside here I have put: pub mod a, and pub mod b
     a.rs
     b.rs
  test_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;

相关问题