2018-12-16 21:21:47 -06:00
|
|
|
struct Point { x: isize, y: isize }
|
2013-12-12 01:17:54 -06:00
|
|
|
|
2018-12-16 21:21:47 -06:00
|
|
|
trait Methods {
|
2013-03-12 21:32:14 -05:00
|
|
|
fn impurem(&self);
|
2015-01-03 09:45:00 -06:00
|
|
|
fn blockm<F>(&self, f: F) where F: FnOnce();
|
2012-07-11 17:00:40 -05:00
|
|
|
}
|
|
|
|
|
2018-12-16 21:21:47 -06:00
|
|
|
impl Methods for Point {
|
2013-03-12 21:32:14 -05:00
|
|
|
fn impurem(&self) {
|
2012-06-01 17:46:32 -05:00
|
|
|
}
|
|
|
|
|
2015-01-03 09:45:00 -06:00
|
|
|
fn blockm<F>(&self, f: F) where F: FnOnce() { f() }
|
2012-06-01 17:46:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn a() {
|
2018-12-16 21:21:47 -06:00
|
|
|
let mut p = Point {x: 3, y: 4};
|
2012-06-01 17:46:32 -05:00
|
|
|
|
2012-06-01 23:54:38 -05:00
|
|
|
// Here: it's ok to call even though receiver is mutable, because we
|
|
|
|
// can loan it out.
|
2012-06-01 17:46:32 -05:00
|
|
|
p.impurem();
|
2012-06-01 23:54:38 -05:00
|
|
|
|
|
|
|
// But in this case we do not honor the loan:
|
2014-02-10 06:44:03 -06:00
|
|
|
p.blockm(|| { //~ ERROR cannot borrow `p` as mutable
|
|
|
|
p.x = 10;
|
2013-11-21 19:23:21 -06:00
|
|
|
})
|
2012-06-01 17:46:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn b() {
|
2018-12-16 21:21:47 -06:00
|
|
|
let mut p = Point {x: 3, y: 4};
|
2012-06-01 17:46:32 -05:00
|
|
|
|
2012-06-01 23:54:38 -05:00
|
|
|
// Here I create an outstanding loan and check that we get conflicts:
|
|
|
|
|
2013-03-15 14:24:24 -05:00
|
|
|
let l = &mut p;
|
|
|
|
p.impurem(); //~ ERROR cannot borrow
|
2012-08-17 16:09:20 -05:00
|
|
|
|
|
|
|
l.x += 1;
|
2012-06-01 17:46:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
}
|