capypad
0 day streak
rust / intermediate
Snippet

Struct Composition and Method Chaining Patterns

Struct composition with multiple constructors and chainable methods creates a clean API. The new() constructor provides simple initialization, while from_hex() offers validation with proper error handling using the ? operator. Method chaining is enabled by methods returning Self, allowing operations like lighten().to_hex(). The saturating_add prevents overflow when lightening colors.

snippet.rs
rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
struct Color {
r: u8,
g: u8,
b: u8,
}
 
impl Color {
fn new(r: u8, g: u8, b: u8) -> Self {
Color { r, g, b }
}
fn from_hex(hex: &str) -> Result<Self, String> {
let hex = hex.trim_start_matches('#');
if hex.len() != 6 {
return Err("Hex must be 6 characters".to_string());
}
let r = u8::from_str_radix(&hex[0..2], 16)
.map_err(|e| format!("Invalid red: {}", e))?;
let g = u8::from_str_radix(&hex[2..4], 16)
.map_err(|e| format!("Invalid green: {}", e))?;
let b = u8::from_str_radix(&hex[4..6], 16)
.map_err(|e| format!("Invalid blue: {}", e))?;
Ok(Color { r, g, b })
}
fn lighten(&self, amount: u8) -> Self {
Color {
r: self.r.saturating_add(amount),
g: self.g.saturating_add(amount),
b: self.b.saturating_add(amount),
}
}
fn to_hex(&self) -> String {
format!("#{:02x}{:02x}{:02x}", self.r, self.g, self.b)
}
}
 
fn main() {
let red = Color::new(255, 0, 0);
let light_red = red.lighten(50);
let blue = Color::from_hex("#0000FF").expect("Invalid hex");
let light_blue = blue.lighten(100);
println!("{} -> {}", red.to_hex(), light_red.to_hex());
println!("{} -> {}", blue.to_hex(), light_blue.to_hex());
}
Breakdown
1
fn from_hex(hex: &str) -> Result<Self, String> {
Alternative constructor with validation returns Result for error handling
2
let r = u8::from_str_radix(&hex[0..2], 16)
Parses hex substring to u8 using base 16 radix
3
.map_err(|e| format!("Invalid red: {}", e))?;
Converts parse error to user-friendly message and propagates
4
self.r.saturating_add(amount)
Prevents overflow; caps at 255 instead of wrapping around