有没有办法像
[a,b] = map
这样分解数组,让这两个数组元素分别移动到 a
和 b
中,以便稍后可以将 a
和 b
移动到另一个函数中(比如在这种情况下的 printme
函数)。enum Direction {
North,
East,
South,
West,
}
struct RoadPoint {
direction: Direction,
index: i32,
}
fn printme(a: RoadPoint, b: RoadPoint) {
println!("First: {}", a.index);
}
fn main() {
let mut map: [RoadPoint; 2] = [
RoadPoint {
direction: Direction::North,
index: 0,
},
RoadPoint {
direction: Direction::North,
index: 0,
},
];
for i in 1..3 {
map[i].index = 10;
}
//move out
printme(map[0], map[1])
}
error[E0508]: cannot move out of type `[RoadPoint; 2]`, a non-copy array
--> src/main.rs:34:13
|
34 | printme(map[0], map[1])
| ^^^^^^ cannot move out of here
error[E0508]: cannot move out of type `[RoadPoint; 2]`, a non-copy array
--> src/main.rs:34:21
|
34 | printme(map[0], map[1])
| ^^^^^^ cannot move out of here
我知道我可以实现Copy
特征,但在这种情况下我实际上不需要数据的副本。因此,我正在寻找更简洁的解决方案。
for point in &mut map { }
)。 - E net4