January 2018
Beginner to intermediate
454 pages
10h 8m
English
The first one works just like OpenOptions:
struct Number(u32);
impl Number {
fn new(nb: u32) -> Number {
Number(nb)
}
fn add(&mut self, other: u32) -> &mut Number {
self.0 += other;
self
}
fn sub(&mut self, other: u32) -> &mut Number {
self.0 -= other;
self
}
fn compute(&self) -> u32 {
self.0
}
}
If you wonder about self.0, just remember that it's how you access a tuple field.
And then you can call it as follow:
let nb = Number::new(0).add(10).sub(5).add(12).compute(); assert_eq!(nb, 17);
This is the first way to do it.
Let's now take a look at the second way to ...