I got a puzzling problem. I've reduced it to this smallest code, but I still can't find a way to solve it. (In a real world 'foo' is not i32, but a complicated structure, and 'bar' is producing a new structure).
struct Foo{
foo: i32
}
impl Foo {
fn bar(self) -> Self{
self
}
}
fn main() {
let mut x: [Option<Foo>;2] = [Some(Foo{foo:1}), Some(Foo{foo:2})];
x[0] = Some(x[0].unwrap().bar());
}
The compiler error is:
|
13 | x[0] = Some(x[0].unwrap().bar());
| ^^^^
| |
| cannot move out of here
| move occurs because `x[_]` has type `std::option::Option<Foo>`, which does not implement the `Copy` trait
Can someone, explain me, how to do it, please? I understand I can't 'move' Option out of array, but how can I replace it?