【发布时间】:2016-11-05 22:36:40
【问题描述】:
我在尝试让 Rust 加载子目录中的文件时遇到问题。 Rust 将文件视为模块,将代码视为模块的一部分,但我习惯于 Ruby 将文件和目录与它们包含的代码分开处理的方式。
src/main.rs
mod lib {
pub mod manifest;
}
src/lib/manifest.rs
mod structs {
pub mod entity;
}
src/lib/structs/entity.rs
pub struct entity {
type: String
}
我得到的错误是:
error: cannot declare a new module at this location
--> src/lib/manifest.rs:2:13
|
2 | pub mod entity;
| ^^^^^^
|
note: maybe move this module `structs` to its own directory via `structs/mod.rs`
--> src/lib/manifest.rs:2:13
|
2 | pub mod entity;
| ^^^^^^
note: ... or maybe `use` the module `entity` instead of possibly redeclaring it
--> src/lib/manifest.rs:2:13
|
2 | pub mod entity;
| ^^^^^^
【问题讨论】:
-
你读过the book chapter on modules吗?如果您解释您对书中的解释有什么问题,我们可能会提供帮助。否则,我们很可能只会重复书中所说的内容。
-
您也可以查看revised book-in-progress,看看这种解释方式是否更适合。
-
谢谢,很有帮助,我的意思是第二个链接。
-
您是否尝试过错误消息说明中的建议?您还尝试了哪些其他方法?您实际上在尝试做什么,为什么要以您尝试的方式构建项目?一个更好的问题形式的标题也可以帮助人们更好地理解问题。