Hello,
I am trying to replace an element in a vector with a new element which can only be constructed after getting ownership of the old element. An example could look like this:
#[derive(Debug)]
struct Foo(Box<i32>);
#[derive(Debug)]
enum FooOrInt {
Foo(Foo),
Int(i32)
}
fn make_int(vec: &mut Vec<FooOrInt>, index: usize) {
vec[index] = FooOrInt::Int(match vec[index] {
FooOrInt::Foo(foo) => *foo.0,
_ => panic!()
});
}
fn main() {
let mut value = vec![FooOrInt::Foo(Foo(Box::new(1))), FooOrInt::Foo(Foo(Box::new(2)))];
println!("{:?}", value);
make_int(&mut value, 1);
println!("{:?}", value);
}
Unfortunately I do not know how this can be achieved. What is a good solution to this problem?