3 > **[FIXME]** What general guidelines should we provide for module design?
5 > We should discuss visibility, nesting, `mod.rs`, and any interesting patterns
8 ### Headers [FIXME: needs RFC]
10 Organize module headers as follows:
11 1. [Imports](../style/imports.md).
12 1. `mod` declarations.
13 1. `pub mod` declarations.
15 ### Avoid `path` directives. [FIXME: needs RFC]
17 Avoid using `#[path="..."]` directives; make the file system and
18 module hierarchy match, instead.
20 ### Use the module hierarchy to organize APIs into coherent sections. [FIXME]
22 > **[FIXME]** Flesh this out with examples; explain what a "coherent
23 > section" is with examples.
25 > The module hierarchy defines both the public and internal API of your module.
26 > Breaking related functionality into submodules makes it understandable to both
27 > users and contributors to the module.
29 ### Place modules in their own file. [FIXME: needs RFC]
32 > - "<100 lines" is arbitrary, but it's a clearer recommendation
33 > than "~1 page" or similar suggestions that vary by screen size, etc.
35 For all except very short modules (<100 lines) and [tests](../testing/README.md),
36 place the module `foo` in a separate file, as in:
41 // in foo.rs or foo/mod.rs
42 pub fn bar() { println!("..."); }
46 rather than declaring it inline:
50 pub fn bar() { println!("..."); }
55 #### Use subdirectories for modules with children. [FIXME: needs RFC]
57 For modules that themselves have submodules, place the module in a separate
58 directory (e.g., `bar/mod.rs` for a module `bar`) rather than the same directory.
61 [`std::io`](https://doc.rust-lang.org/std/io/). Many of the submodules lack
63 [`io::fs`](https://doc.rust-lang.org/std/io/fs/)
65 [`io::stdio`](https://doc.rust-lang.org/std/io/stdio/).
67 [`io::net`](https://doc.rust-lang.org/std/io/net/)
68 contains submodules, so it lives in a separate directory:
84 While it is possible to define all of `io` within a single directory,
85 mirroring the module hierarchy in the directory structure makes
86 submodules of `io::net` easier to find.
88 ### Consider top-level definitions or reexports. [FIXME: needs RFC]
90 For modules with submodules,
91 define or [reexport](https://doc.rust-lang.org/std/io/#reexports) commonly used
92 definitions at the top level:
94 * Functionality relevant to the module itself or to many of its
95 children should be defined in `mod.rs`.
96 * Functionality specific to a submodule should live in that
97 submodule. Reexport at the top level for the most important or
101 [`IoError`](https://doc.rust-lang.org/std/io/struct.IoError.html)
102 is defined in `io/mod.rs`, since it pertains to the entirety of `io`,
104 [`TcpStream`](https://doc.rust-lang.org/std/io/net/tcp/struct.TcpStream.html)
105 is defined in `io/net/tcp.rs` and reexported in the `io` module.
107 ### Use internal module hierarchies for organization. [FIXME: needs RFC]
110 > - Referencing internal modules from the standard library is subject to
113 Internal module hierarchies (i.e., private submodules) may be used to
114 hide implementation details that are not part of the module's API.
116 For example, in [`std::io`](https://doc.rust-lang.org/std/io/), `mod mem`
117 provides implementations for
118 [`BufReader`](https://doc.rust-lang.org/std/io/struct.BufReader.html)
120 [`BufWriter`](https://doc.rust-lang.org/std/io/struct.BufWriter.html),
121 but these are re-exported in `io/mod.rs` at the top level of the module:
126 pub use self::mem::{MemReader, BufReader, MemWriter, BufWriter};
131 This hides the detail that there even exists a `mod mem` in `io`, and
132 helps keep code organized while offering freedom to change the