2012-12-10 19:32:48 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-03-06 21:09:17 -06:00
|
|
|
struct Point {
|
|
|
|
x: int,
|
|
|
|
y: int,
|
|
|
|
}
|
2012-06-01 17:46:32 -05:00
|
|
|
|
|
|
|
fn a() {
|
2013-02-25 19:28:40 -06:00
|
|
|
let mut p = ~[1];
|
2012-06-01 17:46:32 -05:00
|
|
|
|
|
|
|
// Create an immutable pointer into p's contents:
|
2012-06-30 06:23:59 -05:00
|
|
|
let _q: &int = &p[0]; //~ NOTE loan of mutable vec content granted here
|
2012-06-01 17:46:32 -05:00
|
|
|
|
2012-06-30 06:23:59 -05:00
|
|
|
p[0] = 5; //~ ERROR assigning to mutable vec content prohibited due to outstanding loan
|
2012-06-01 17:46:32 -05:00
|
|
|
}
|
|
|
|
|
2013-03-07 16:38:38 -06:00
|
|
|
fn borrow(_x: &[int], _f: &fn()) {}
|
2012-06-01 17:46:32 -05:00
|
|
|
|
|
|
|
fn b() {
|
|
|
|
// here we alias the mutable vector into an imm slice and try to
|
|
|
|
// modify the original:
|
|
|
|
|
2013-02-25 19:28:40 -06:00
|
|
|
let mut p = ~[1];
|
2012-06-01 17:46:32 -05:00
|
|
|
|
2012-07-04 14:04:28 -05:00
|
|
|
do borrow(p) { //~ NOTE loan of mutable vec content granted here
|
2012-06-30 06:23:59 -05:00
|
|
|
p[0] = 5; //~ ERROR assigning to mutable vec content prohibited due to outstanding loan
|
2012-06-01 17:46:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn c() {
|
|
|
|
// Legal because the scope of the borrow does not include the
|
|
|
|
// modification:
|
2013-02-25 19:28:40 -06:00
|
|
|
let mut p = ~[1];
|
2012-06-30 18:19:07 -05:00
|
|
|
borrow(p, ||{});
|
2012-06-01 17:46:32 -05:00
|
|
|
p[0] = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
}
|
|
|
|
|