Skip to content

Commit 46d9e74

Browse files
committed
modules: add explanatory commentary to solution
This commentary, written by Gemini, focuses on aspects of the solution that differ from the baseline languages (C/Java/Python), highlighting Rust-specific idioms and concepts.
1 parent 0867e24 commit 46d9e74

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/modules/solution.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,26 @@ fn main() {
186186
window.draw();
187187
}
188188
```
189+
190+
- **File Structure:** We use a directory `src/widgets/` for sub-modules and a file
191+
`src/widgets.rs` to define the module itself. This is the standard way to
192+
structure modules in modern Rust (the 2018 edition and later).
193+
- **Visibility:** We use `pub` to make `Button`, `Label`, `Window`, and `Widget`
194+
accessible from outside the `widgets` module. Fields of structs (like
195+
`Label.label`) remain private by default, preserving encapsulation.
196+
- **Re-exports:** In `src/widgets.rs`, we use `pub use button::Button;`. This
197+
re-exports `Button` from the `widgets` module, so users can import it as
198+
`widgets::Button` rather than `widgets::button::Button`. This creates a cleaner
199+
public API.
200+
- **Relative Imports:** The sub-modules (like `label.rs`) use `use super::Widget;`
201+
to access the `Widget` trait defined in the parent module.
202+
203+
<details>
204+
205+
- Note that we could have also put `mod` declarations in `src/main.rs` directly
206+
referencing the files, but grouping them under a `widgets` module is cleaner
207+
for a library.
208+
- Discuss how `mod.rs` (the older style) is also supported but less common in new
209+
code. `src/widgets/mod.rs` would be equivalent to `src/widgets.rs`.
210+
211+
</details>

0 commit comments

Comments
 (0)