I have a enum, which may contains a vector. And I implement a method for the enum, which can generator a new enum which reuse the same vector in previous enum (I don't want to copy the vector). After generated the new enum, I won't use the previous enum anymore, so I implement this method as fn (self)
which would get enum's ownership.
Then I put enum in a structure, I want to replace the enum in the structure by using the enum's method I just implemented. But I got this error:
error[E0507]: cannot move out of borrowed content
--> src/lib.rs:22:18
|
22 | self.0 = self.0.get_a_new_foo();
| ^^^^^^ cannot move out of borrowed content
Is there any way to fix my code?
enum Foo {
A,
B(Vec<u32>),
}
impl Foo {
fn get_a_new_foo(self) -> Foo {
match self {
Foo::A => Foo::B(vec![]),
Foo::B(mut v) => {
let len = v.len() as u32;
v.push(len - 1);
Foo::B(v)
}
}
}
}
struct Bar(Foo);
impl Bar {
fn replace_foo(&mut self) -> () {
self.0 = self.0.get_a_new_foo();
}
}