我擁有一個數組(固定大小= 3)的所有權,並且我想對它進行迭代,並隨時移動元素。基本上,我想爲固定大小的陣列實施IntoIterator
。將對象移出陣列
由於數組沒有在標準庫中實現這個特徵(我理解爲什麼),是否有解決方法來獲得所需的效果?我的物品不是Copy
也不是Clone
。我可以在陣列中創建一個Vec
,然後迭代到Vec
,但我甚至不知道該怎麼做。
(有關信息,我想履行的Complete
陣列)
這裏是的情況一個簡單的例子(與幼稚iter()
嘗試):http://is.gd/fxN7Yw
// No-copy, No-clone struct
#[derive(Debug)]
struct Foo;
// A method that needs an owned Foo
fn bar(foo: Foo) { println!("{:?}", foo); }
fn main() {
let v: [Foo; 3] = [Foo,Foo,Foo];
for a in v.iter() {
bar(*a);
}
}
給出
<anon>:13:13: 13:15 error: cannot move out of borrowed content [E0507]
<anon>:13 bar(*a);
^~
error: aborting due to previous error
謝謝。所以沒有安全的方法可以單獨使用'Array',但是使用'ArrayVec'可能會影響它與'Vec'之間的折衷。 – Gyscos