RUST
Free Section Starter Challenge
40 min
5. Structs & Method Implementations (impl)
Overview: Organize data and behavior in Rust with custom structs and impl blocks, using &self for safe method calls.
Concept & Syntax Guide
In Rust, structs hold data while `impl` blocks define associated functions and methods. Borrowing `&self` guarantees memory safety without garbage collection overhead.
Hands-On Practice Exercise
Calculate Bounding Box Area in Rust
Define a `Rectangle` struct with `width: u32` and `height: u32`. Implement a method `area(&self) -> u32`. Create an instance and print the area.
Target Output:
Rectangle Area: 1500
Need a hint? Click here to reveal
Hint: Check syntax carefully and make sure your code contains:
Rectangle Area: 1500
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10