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