summaryrefslogtreecommitdiffstats
path: root/tests/ui/borrowck/two-phase-across-loop.rs
blob: 3fcea7d171349d6ac6171faf3611bed0fa7eb4c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Test that a borrow which starts as a two-phase borrow and gets
// carried around a loop winds up conflicting with itself.

struct Foo { x: String }

impl Foo {
    fn get_string(&mut self) -> &str {
        &self.x
    }
}

fn main() {
    let mut foo = Foo { x: format!("Hello, world") };
    let mut strings = vec![];

    loop {
        strings.push(foo.get_string()); //~ ERROR cannot borrow `foo` as mutable
        if strings.len() > 2 { break; }
    }

    println!("{:?}", strings);
}