Hello,
I have some structure (actually a vector) wrapped in an enum and I would like to define a method bar(&mut self)
that allows me to :
- Swap the wrapped vector with another one constructed with the previous one, without creating any copy of anything on the way.
- Change the enum pattern of self
Here is an example of what I would like to do, because I admit it seems pretty unclear :
#[derive(Debug)]
enum Foo {
A(Vec<i32>), // Here is the wrapped structure
B(Vec<Vec<i32>>)
}
use Foo::{A, B};
impl Foo {
fn bar(&mut self) {
match self {
A(vect) => *self = B(vec![*vect]),
_ => ()
}
}
}
fn main() {
let mut ex: Foo = A(vec![1]);
ex.bar();
println!("{:?}", ex);
}
Now, this does not work for me because I'm moving the ownership of *vect
to the method bar
. Of course I could use a .clone()
but my question is whether I can do this without copying my whole vector on another place of the heap or not (that's what cloning does, right ?).
I suppose this may be possible since I can do something similar by returning a value rather than modifying self
:
impl Foo {
fn bar(self) -> Foo {
match self {
A(vect) => B(vec![vect]),
_ => self
}
}
}
fn main() {
let mut ex: Foo = A(vec![1]);
ex = ex.bar();
println!("{:?}", ex);
}
Thank you for taking your time to answer my question I did not find any conversation related to this specific topic but maybe I just did not search well, sorry if so.