2014-12-22 11:04:23 -06:00
|
|
|
use std::ops::Add;
|
|
|
|
|
2015-03-30 08:38:27 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2013-03-15 14:24:24 -05:00
|
|
|
struct Point {
|
2015-01-08 04:54:35 -06:00
|
|
|
x: isize,
|
|
|
|
y: isize,
|
2012-07-27 21:32:42 -05:00
|
|
|
}
|
2012-06-01 23:54:38 -05:00
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
impl Add<isize> for Point {
|
|
|
|
type Output = isize;
|
2014-12-31 14:45:13 -06:00
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
fn add(self, z: isize) -> isize {
|
2014-12-01 16:52:10 -06:00
|
|
|
self.x + self.y + z
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
|
|
|
}
|
2012-07-11 17:00:40 -05:00
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl Point {
|
2015-01-08 04:54:35 -06:00
|
|
|
pub fn times(&self, z: isize) -> isize {
|
2012-07-27 21:32:42 -05:00
|
|
|
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:
|
|
|
|
|
2013-03-15 14:24:24 -05:00
|
|
|
let q = &mut p;
|
2012-06-01 23:54:38 -05:00
|
|
|
|
2014-12-01 16:52:10 -06:00
|
|
|
p + 3; //~ ERROR cannot use `p`
|
2013-03-15 14:24:24 -05:00
|
|
|
p.times(3); //~ ERROR cannot borrow `p`
|
2012-08-17 16:09:20 -05:00
|
|
|
|
2013-05-01 12:48:00 -05:00
|
|
|
*q + 3; // OK to use the new alias `q`
|
|
|
|
q.x += 1; // and OK to mutate it
|
2012-06-01 23:54:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
}
|