首页 > 其他 > 详细

rust borrow and move

时间:2015-01-12 20:40:12      阅读:324      评论:0      收藏:0      [点我收藏+]
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);
}

 

rust borrow and move

原文:http://www.cnblogs.com/lightlfyan/p/4219010.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!