1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
struct Counter { value: u64, // <1> } impl Counter { fn new() -> Self { // <2> <3> Counter { value: 0 } // <4> <5> } fn incr(&mut self) { // <6> self.value += 1; } } fn main() { let mut counter = Counter::new(); counter.incr(); counter.incr(); println!("{}", counter.value); }