Complete these tasks to reinforce what you learned in this module.
In src/bin/task_01.rs, define an inline module named geometry that contains two functions:
- pub fn area(width: u32, height: u32) -> u32 returning width * height
- pub fn perimeter(width: u32, height: u32) -> u32 returning 2 * (width + height)
Both functions must be pub so main can reach them. In main, call them through their module path (geometry::area(...) / geometry::perimeter(...)) with width = 4 and height = 3, and print the two results.
mod geometry { ... }. Without pub, the functions are private to geometry and main cannot call them (private by default — see the theory). Refer to the items as geometry::area(4, 3) and geometry::perimeter(4, 3).// Task 1: Organize functions into an inline module and call them via paths.
// See tasks/TASKS.md.
// Run: cargo run -p course_37_module_system --bin task_01
fn main() {
// TODO: Define `mod geometry { ... }` with pub fns `area` and `perimeter`,
// then call them with width = 4, height = 3 and print:
// area = 12
// perimeter = 14
todo!("Create mod geometry with pub area/perimeter and call via geometry::...");
}
In src/bin/task_02.rs, build a small module tree that exercises crate-level visibility, the super:: path, and a use import:
1. Define a module store containing a function pub(crate) fn tax_rate() -> u32 that returns 8 (this is the C# internal equivalent).
2. Inside store, define a nested module pub mod checkout with pub fn total(price: u32) -> u32 that returns price + price * super::tax_rate() / 100, calling the parent's tax_rate via super::.
3. At the top of the file, bring the nested function into scope with use store::checkout::total;, then in main call total (unqualified, thanks to the use) for prices 100 and 200 and print both results.
pub(crate) makes tax_rate visible across the crate but not part of any external API. super::tax_rate() from inside checkout walks up to its parent module store. The use store::checkout::total; line is what lets you write total(100) instead of the full store::checkout::total(100). Math: 100 + 100 * 8 / 100 = 108 and 200 + 200 * 8 / 100 = 216.// Task 2: pub(crate), super::, and use. See tasks/TASKS.md.
// Run: cargo run -p course_37_module_system --bin task_02
fn main() {
// TODO: Build `mod store` with `pub(crate) fn tax_rate() -> u32` (returns 8)
// and a nested `pub mod checkout` whose `pub fn total(price)` uses
// super::tax_rate(). Bring it into scope with `use store::checkout::total;`
// and print:
// total(100) = 108
// total(200) = 216
todo!("Build store::checkout::total using pub(crate), super::, and a use import");
}