码迷,mamicode.com
首页 > 其他好文 > 详细

rust borrow and move

时间:2015-01-12 20:43:27      阅读:176      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!