Why is there a mod keyword in Rust?

2019-06-22 08:52发布

After reading this I'm wondering why is there "mod" keyword and mod.rs? The folder hierarchy can describe the module as well?

标签: module rust
2条回答
Lonely孤独者°
2楼-- · 2019-06-22 09:41

While it can, it can also be overridden:

#[path = "somewhere/else"]
mod lol;
查看更多
老娘就宠你
3楼-- · 2019-06-22 09:42

There are a couple of reasons why modules must be explicitly declared:

  • Modules can be public (pub mod foo;) or private (mod foo;).

  • They can have attributes applied to them, attributes that couldn’t sit inside the file; there are two primary examples of that: #[path = "x.rs"] specifying a different path, and #[cfg(…)], for conditional compilation, for cases where the module would fail to parse or have its macros expand.

查看更多
登录 后发表回答