Modules Cheat Sheet is a quick reference on how modules, paths, the use keyword, and the pub keyword work in the Rust compiler. Here is an example of how to use Modules Cheat Sheet:
mod garden;
pub mod vegetables;
fn main() {
use crate::garden::vegetables::Asparagus;
let plant = Asparagus {};
println!("I'm growing {:?}!", plant);
}
This code will compile and print the following output:
I'm growing Asparagus!
The `mod garden;` line tells the compiler to include the code it finds in `src/garden.rs`. The `pub mod vegetables;` line means the code in `src/garden/vegetables.rs` is included too. That code is:
#[derive(Debug)]
pub struct Asparagus {}
The `use crate::garden::vegetables::Asparagus;` line creates a shortcut to the `Asparagus` struct, so we can refer to it as `Asparagus` instead of `crate::garden::vegetables::Asparagus`.
The `main()` function then creates a new `Asparagus` struct and prints it out.