2012-07-27 21:32:42 -05:00
|
|
|
struct Point {
|
2012-09-07 16:50:47 -05:00
|
|
|
x: int,
|
|
|
|
y: int,
|
2012-07-27 21:32:42 -05:00
|
|
|
}
|
2012-06-01 23:54:38 -05:00
|
|
|
|
2012-09-19 20:00:26 -05:00
|
|
|
impl Point : ops::Add<int,int> {
|
2012-12-06 13:08:23 -06:00
|
|
|
pure fn add(&self, z: &int) -> int {
|
2012-09-19 20:00:26 -05:00
|
|
|
self.x + self.y + (*z)
|
|
|
|
}
|
|
|
|
}
|
2012-07-11 17:00:40 -05:00
|
|
|
|
2012-07-27 21:32:42 -05:00
|
|
|
impl Point {
|
|
|
|
fn times(z: int) -> int {
|
|
|
|
self.x * self.y * z
|
|
|
|
}
|
2012-06-01 23:54:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn a() {
|
2012-07-27 21:32:42 -05:00
|
|
|
let mut p = Point {x: 3, y: 4};
|
2012-06-01 23:54:38 -05:00
|
|
|
|
|
|
|
// ok (we can loan out rcvr)
|
|
|
|
p + 3;
|
2012-07-27 21:32:42 -05:00
|
|
|
p.times(3);
|
2012-06-01 23:54:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn b() {
|
2012-07-27 21:32:42 -05:00
|
|
|
let mut p = Point {x: 3, y: 4};
|
2012-06-01 23:54:38 -05:00
|
|
|
|
|
|
|
// Here I create an outstanding loan and check that we get conflicts:
|
|
|
|
|
2012-08-17 16:09:20 -05:00
|
|
|
let q = &mut p; //~ NOTE prior loan as mutable granted here
|
2012-06-01 23:54:38 -05:00
|
|
|
|
2012-12-06 16:53:21 -06:00
|
|
|
p + 3; // ok for pure fns
|
2012-07-27 21:32:42 -05:00
|
|
|
p.times(3); //~ ERROR loan of mutable local variable as immutable conflicts with prior loan
|
2012-08-17 16:09:20 -05:00
|
|
|
|
|
|
|
q.x += 1;
|
2012-06-01 23:54:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn c() {
|
|
|
|
// Here the receiver is in aliased memory and hence we cannot
|
|
|
|
// consider it immutable:
|
2012-07-27 21:32:42 -05:00
|
|
|
let q = @mut Point {x: 3, y: 4};
|
2012-06-01 23:54:38 -05:00
|
|
|
|
|
|
|
// ...this is ok for pure fns
|
|
|
|
*q + 3;
|
|
|
|
|
|
|
|
|
|
|
|
// ...but not impure fns
|
2012-09-11 23:25:01 -05:00
|
|
|
(*q).times(3); //~ ERROR illegal borrow unless pure
|
2012-06-30 06:23:59 -05:00
|
|
|
//~^ NOTE impure due to access to impure function
|
2012-06-01 23:54:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
}
|
|
|
|
|