• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 struct Point { x: isize, y: isize }
2 
3 trait Methods {
impurem(&self)4     fn impurem(&self);
blockm<F>(&self, f: F) where F: FnOnce()5     fn blockm<F>(&self, f: F) where F: FnOnce();
6 }
7 
8 impl Methods for Point {
impurem(&self)9     fn impurem(&self) {
10     }
11 
blockm<F>(&self, f: F) where F: FnOnce()12     fn blockm<F>(&self, f: F) where F: FnOnce() { f() }
13 }
14 
a()15 fn a() {
16     let mut p = Point {x: 3, y: 4};
17 
18     // Here: it's ok to call even though receiver is mutable, because we
19     // can loan it out.
20     p.impurem();
21 
22     // But in this case we do not honor the loan:
23     p.blockm(|| { //~ ERROR cannot borrow `p` as mutable
24         p.x = 10;
25     })
26 }
27 
b()28 fn b() {
29     let mut p = Point {x: 3, y: 4};
30 
31     // Here I create an outstanding loan and check that we get conflicts:
32 
33     let l = &mut p;
34     p.impurem(); //~ ERROR cannot borrow
35 
36     l.x += 1;
37 }
38 
main()39 fn main() {
40 }
41