rust/tests/ui/borrowck/borrowck-rvalues-mutable.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

35 lines
553 B
Rust
Raw Normal View History

// run-pass
2013-05-16 14:10:50 -05:00
struct Counter {
value: usize
2013-05-16 14:10:50 -05:00
}
impl Counter {
fn new(v: usize) -> Counter {
2013-05-16 14:10:50 -05:00
Counter {value: v}
}
fn inc<'a>(&'a mut self) -> &'a mut Counter {
self.value += 1;
self
}
fn get(&self) -> usize {
self.value
}
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);
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
}