bitnick
bitnick

Reputation: 2073

Why is there a mod keyword in Rust?

After reading this, I'm wondering why there is a mod keyword and mod.rs?

I assumed that the directory hierarchy can describe the module as well.

Upvotes: 11

Views: 4826

Answers (2)

Chris Morgan
Chris Morgan

Reputation: 90712

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.

Upvotes: 17

Steve Klabnik
Steve Klabnik

Reputation: 15529

While it can, it can also be overridden:

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

Upvotes: 8

Related Questions