【发布时间】:2019-01-29 21:17:09
【问题描述】:
有没有办法像[a, b] = map这样解构数组,让两个数组元素移动到a和b,这样以后a和b就可以移动到另一个函数中(比如@ 987654326@ 在这种情况下)。
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 { })。
标签: rust