Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/modules/solution.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,27 @@ fn main() {
window.draw();
}
```

- **File Structure:** We use a directory `src/widgets/` for sub-modules and a
file `src/widgets.rs` to define the module itself. This is the standard way to
structure modules in modern Rust (the 2018 edition and later).
- **Visibility:** We use `pub` to make `Button`, `Label`, `Window`, and `Widget`
accessible from outside the `widgets` module. Fields of structs (like
`Label.label`) remain private by default, preserving encapsulation.
- **Re-exports:** In `src/widgets.rs`, we use `pub use button::Button;`. This
re-exports `Button` from the `widgets` module, so users can import it as
`widgets::Button` rather than `widgets::button::Button`. This creates a
cleaner public API.
- **Relative Imports:** The sub-modules (like `label.rs`) use
`use super::Widget;` to access the `Widget` trait defined in the parent
module.

<details>

- Note that we could have also put `mod` declarations in `src/main.rs` directly
referencing the files, but grouping them under a `widgets` module is cleaner
for a library.
- Discuss how `mod.rs` (the older style) is also supported but less common in
new code. `src/widgets/mod.rs` would be equivalent to `src/widgets.rs`.

</details>