普通的數組數據交換,數組
let mut val = [1,2,3];
let t = val[0];
val[0] = val[1];
val[1] = t;spa
it works...下面的也work:string
let mut val = ["1","2","3"];
let t = val[0];
val[0] = val[1];
val[1] = t;it
但,io
let mut val = ["1".to_string(),"2".to_string(),"3".to_string()];
let t = val[0];
val[0] = val[1];
val[1] = t;編譯
編譯出錯:event
<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
正確的交換方式是,class
val.swap(0,1);
rust