Creating Modules
In the previous section, we defined an enum
and struct
. However, we didn't
actually create a module! In this section, we will. Before diving into the
details, the Rust documentation provides a Module Cheat Sheet which will be a
handy reference on your Rust journey.
Since we're building our module to work in this online book, we'll deviate
slightly from standard practice. Typically, modules are created in separate
files. For instance, our Interval
would be placed in a new file called
interval.rs
.
grep
├─ Cargo.lock
├─ Cargo.toml
└─ src
├─ interval.rs
└─ main.rs
Document generation
As we develop our
interval
module, it's a great opportunity to learn aboutrustdoc
and documentation generation. Theinterval
module will be annotated to enable documentation generation. If you're building the code locally, the simplest way to generate and view documentation is by usingcargo doc --open
.
Let's get started!