Why is there a mod keyword in Rust?

2019-06-22 08:54发布

问题:

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

回答1:

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.



回答2:

While it can, it can also be overridden:

#[path = "somewhere/else"]
mod lol;


标签: module rust