gpt4 book ai didi

rust - 有没有办法将多个非复制元素移出数组?

转载 作者:行者123 更新时间:2023-11-29 08:08:33 26 4
gpt4 key购买 nike

<分区>

有没有办法解构像[a, b] = map这样的数组,把两个数组元素移到ab,以便稍后可以将 ab 移到另一个函数中(如本例中的 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 特性,但在这种情况下我实际上不需要数据副本。因此,我正在寻找更清洁的解决方案。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com