rust borrow and move

extern crate core;

#[deriving(Show)]
struct Foo {
    f : Box<int>
}

fn main(){
    let mut a = Foo {f: box 0};
    let y : &Foo;
    
    // out(&a);
    {
        let b = Foo {f: box 10};
        // b does not live long enough
        // can not y = &b;

        let  c = & mut a;
        
        // a borrowed by c
        //a.f = box 11;

        // c moved by y
        y = c;

        println!("{}", c.f);

        // cannot move out of `c` because it is borrowed
        //let c1 = c;
    }
    //a.f = box 11;
    println!("{}", y.f);
}

fn out(v: &Foo) {
    // borrow
    println!("{}", v);
}

fn test2(){
    let mut a:  Box<int> = box 1;
    // shared borrow
    let b = &a;
    let c = b;
    println!("{}", *b);
}
原文地址:https://www.cnblogs.com/lightlfyan/p/4219010.html