2023-11-29 10:39:24 -05:00
---
minutes: 5
---
2022-12-21 16:36:30 +01:00
# Filesystem Hierarchy
2023-07-18 13:32:45 +02:00
Omitting the module content will tell Rust to look for it in another file:
2022-12-21 16:36:30 +01:00
```rust,editable,compile_fail
mod garden;
```
2023-07-18 13:32:45 +02:00
This tells rust that the `garden` module content is found at `src/garden.rs` .
Similarly, a `garden::vegetables` module can be found at `src/garden/vegetables.rs` .
2022-12-21 16:36:30 +01:00
The `crate` root is in:
* `src/lib.rs` (for a library crate)
* `src/main.rs` (for a binary crate)
2023-02-16 03:19:25 +00:00
2023-03-21 12:15:07 -04:00
Modules defined in files can be documented, too, using "inner doc comments".
These document the item that contains them -- in this case, a module.
```rust,editable,compile_fail
//! This module implements the garden, including a highly performant germination
//! implementation.
2023-04-24 14:51:23 -04:00
// Re-export types from this module.
pub use seeds::SeedPacket;
pub use garden::Garden;
2023-03-21 12:15:07 -04:00
/// Sow the given seed packets.
2023-04-24 14:51:23 -04:00
pub fn sow(seeds: Vec< SeedPacket > ) { todo!() }
2023-03-21 12:15:07 -04:00
2023-04-24 14:51:23 -04:00
/// Harvest the produce in the garden that is ready.
pub fn harvest(garden: & mut Garden) { todo!() }
2023-03-21 12:15:07 -04:00
```
2023-02-16 03:19:25 +00:00
< details >
2023-07-18 13:32:45 +02:00
* Before Rust 2018, modules needed to be located at `module/mod.rs` instead of `module.rs` , and this is still a working alternative for editions after 2018.
2023-02-16 03:19:25 +00:00
2023-07-18 13:32:45 +02:00
* The main reason to introduce `filename.rs` as alternative to `filename/mod.rs`
was because many files named `mod.rs` can be hard to distinguish in IDEs.
* Deeper nesting can use folders, even if the main module is a file:
2023-02-16 03:19:25 +00:00
```ignore
src/
├── main.rs
├── top_module.rs
└── top_module/
└── sub_module.rs
```
2023-07-18 13:32:45 +02:00
* The place rust will look for modules can be changed with a compiler directive:
2023-02-16 03:19:25 +00:00
```rust,ignore
#[path = "some/path.rs"]
2023-07-17 16:54:47 +02:00
mod some_module;
2023-02-16 03:19:25 +00:00
```
This is useful, for example, if you would like to place tests for a module in a file named
`some_module_test.rs` , similar to the convention in Go.
< / details >