【发布时间】:2021-04-10 14:47:23
【问题描述】:
我的文件结构如下:
- 源
- function.rs
- main.rs
- misc_helpers.rs
- method.rs
在main.rs 中,我执行以下操作:
mod misc_helpers;
mod method;
mod function;
因为我使用这些模块中的函数。现在在method.rs 中,我使用来自misc_helpers.rs 和function.rs 的函数。要包含这些文件,我想使用 mod(就像在 main.rs 中一样),但它给了我错误:
file not found for module `misc_helpers`
help: to create the module `misc_helpers`, create file "src/method/misc_helpers.rs"
我发现我有 2 个解决方案:
(方案一)src/method.rs
use super::misc_helpers;
use super::function::*;
(解决方案2)src/method.rs
#[path = "misc_helpers.rs"] mod misc_helpers;
#[path = "function.rs"] mod function;
use function::*;
所以我有 3 个问题:
- 为什么在
method.rs中我不能只使用mod而不使用path(就像在main.rs中一样)?我想main.rs应该被视为cargo模块(即根模块),我只能使用mod来包含“子”模块(在模块层次结构中更深)而不是“父”模块?李> - 为什么
use super::工作(在method.rs中)即使我没有包含这些文件? - 什么是更好的解决方案(1 或 2),为什么?
【问题讨论】:
-
还有
use crate::misc_helpers;的选项。 -
@Thomas 是的,这是真的。
标签: rust