Method chaining

 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
trait CanCalc {
fn calc(&self) -> i32;
}
struct Man {
name: &'static str
}
impl Man {
fn calc(&self) -> i32 {
6
}
}
struct Nerd {
name: &'static str,
parent: Man
}
impl CanCalc for Nerd {
fn calc(&self) -> i32 {
let var = self.parent.calc();
println!("{}",var);
return 67+var;
}
}
fn main() {
let i: Nerd = Nerd{name: "nerd", parent: Man{name: "man"}};
println!("{}",i.calc());
}