首页 > 其他 > 详细

Rust Notes - 01

时间:2015-06-12 00:51:46      阅读:155      评论:0      收藏:0      [点我收藏+]

普通的数组数据交换,

    let mut val = [1,2,3];

    let t = val[0];
    val[0] = val[1];
    val[1] = t;

it works...下面的也work:

    let mut val = ["1","2","3"];

    let t = val[0];
    val[0] = val[1];
    val[1] = t;

但,

   let mut val = ["1".to_string(),"2".to_string(),"3".to_string()];
   
    let t = val[0];
    val[0] = val[1];
    val[1] = t;

编译出错:

<anon>:32:13: 32:19 error: cannot move out of type `[collections::string::String; 3]`, a non-copy fixed-size array
<anon>:32     let t = val[0];
                      ^~~~~~
<anon>:32:9: 32:10 note: attempting to move value to here
<anon>:32     let t = val[0];
                  ^
<anon>:32:9: 32:10 help: to prevent the move, use `ref t` or `ref mut t` to capture value by reference
<anon>:33:14: 33:20 error: cannot move out of type `[collections::string::String; 3]`, a non-copy fixed-size array
<anon>:33     val[0] = val[1];
                       ^~~~~~
error: aborting due to 2 previous errors

  正确的交换方式是,


    val.swap(0,1);
   

Rust Notes - 01

原文:http://www.cnblogs.com/rxlinq/p/4570538.html

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