2018-09-04 05:05:53 -05:00
|
|
|
// run-pass
|
2015-03-22 15:13:15 -05:00
|
|
|
|
2013-05-16 14:10:50 -05:00
|
|
|
struct Counter {
|
2015-03-25 19:06:52 -05:00
|
|
|
value: usize
|
2013-05-16 14:10:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Counter {
|
2015-03-25 19:06:52 -05:00
|
|
|
fn new(v: usize) -> Counter {
|
2013-05-16 14:10:50 -05:00
|
|
|
Counter {value: v}
|
|
|
|
}
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
fn inc<'a>(&'a mut self) -> &'a mut Counter {
|
|
|
|
self.value += 1;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn get(&self) -> usize {
|
2014-01-15 13:39:08 -06:00
|
|
|
self.value
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn get_and_inc(&mut self) -> usize {
|
2013-05-16 14:10:50 -05:00
|
|
|
let v = self.value;
|
|
|
|
self.value += 1;
|
|
|
|
v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
let v = Counter::new(22).get_and_inc();
|
|
|
|
assert_eq!(v, 22);
|
2014-01-15 13:39:08 -06:00
|
|
|
|
|
|
|
let v = Counter::new(22).inc().inc().get();
|
2015-09-27 21:26:12 -05:00
|
|
|
assert_eq!(v, 24);
|
2013-05-16 14:10:50 -05:00
|
|
|
}
|