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