From 0b5c39c339aa294e5ea111b605482f2f97e67e37 Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Sat, 31 Jan 2026 13:04:06 +0100 Subject: [PATCH 1/2] 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. --- src/modules/solution.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/modules/solution.md b/src/modules/solution.md index e8fd9ceaa5a0..eee3665d3504 100644 --- a/src/modules/solution.md +++ b/src/modules/solution.md @@ -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. + +
+ +- 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`. + +
From 69b17e2640b8d7117a722b74d7de3cca6043fd50 Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Sat, 14 Feb 2026 10:54:32 +0100 Subject: [PATCH 2/2] Refine solution commentary for experienced programmers - Remove redundant or overly simplistic explanations. - Focus on Rust-specific idioms and design choices. - Clean up formatting and technical depth. --- src/modules/solution.md | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/modules/solution.md b/src/modules/solution.md index eee3665d3504..45959ce2dc04 100644 --- a/src/modules/solution.md +++ b/src/modules/solution.md @@ -187,26 +187,25 @@ fn main() { } ``` -- **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. +The solution demonstrates the standard Rust module hierarchy and visibility +rules: + +- **Module Hierarchy:** Sub-modules are placed in a directory (`src/widgets/`) + and declared in a corresponding file (`src/widgets.rs`). +- **Encapsulation:** Visibility is controlled with `pub`. Fields remain private + by default, while types and the `Widget` trait are exposed for external use. +- **Re-exports:** Using `pub use` in `src/widgets.rs` provides a flat public + API, allowing users to import `widgets::Button` instead of the internal + `widgets::button::Button`. +- **Path Resolution:** Sub-modules use `super::Widget` to refer to items in the + parent module.
-- 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`. +- **Module Styles:** This structure uses the modern (Rust 2018+) module style. + Previously, `src/widgets/mod.rs` was required instead of `src/widgets.rs`. +- **API Design:** Re-exporting is a powerful tool for decoupling your internal + code organization from your library's public interface, allowing for + refactoring without breaking changes.