How to take out the variable included in the enum type in case of consuming the enum

I meet a problem about enum, code as follows

#[derive(Debug, Clone)]
struct Mat {
  d: Vec<Vec<f64>>,
  row: usize,
  col: usize,
}
struct Vector(Vec<f64>);
enum CompoundType {
   ...
   Matrix(Mat),
   ...
}
impl From <CompoundType> for Vec<Vector> {
   fn from(t: CompoundType)-> Self {
        if let CompoundType::Matrix(m) = t {
            return m.into_iter().map(|v| Vector::new(v)).collect();
        } else {
           ...
        }
   }
}

compile error

error[E0507]: cannot move out of dereference of `toolkit::Mat`
   --> src\toolkit.rs:468:20
468 | return m.into_iter().map(|v| Vector::new(v)).collect();
    |        ^ move occurs because value has type `Vec<Vec<f64>>`, which does not implement the `Copy` trait

In more general case, how can I take out the variable included in the enum type since I will consume this enum.
please help, thanks.

The code you posted here should be fine, but because it consumes t, you won't be able to use t again later in the function. Can you post the complete error message, and all the code that it refers to? (The message you posted is just one part of a longer error.)

1 Like

I don’t even think this is true, since the return statement makes sure that later in the function – even outside of (i.e. after) the else branch – t has not been consumed yet.

1 Like

Yes, good point. So maybe in the actual code, t was already consumed earlier in the function.

thanks for reply. all error as follows

error[E0507]: cannot move out of dereference of `toolkit::Matrix`
   --> src\toolkit.rs:468:20
    |
468 |             return m.into_iter().map(|v| Vector::new(v)).collect();
    |                    ^ move occurs because value has type `Vec<Vec<f64>>`, which does not implement the `Copy` trait

error: aborting due to previous error

I think so, t has not been consumed because if I change m.into_iter() to m.iter(), it will be works.
the problem maybe that do not allow to take out m from Matrix(m), but if I want to consume Matrix(m) and convert to m, how can I do?

Your actual code is probably a bit different than what you posted, because this works:

struct Vector(Vec<f64>);
enum CompoundType {
   Matrix(Vec<Vec<f64>>),
   Other
}
impl From<CompoundType> for Vec<Vector> {
   fn from(t: CompoundType) -> Self {
        if let CompoundType::Matrix(m) = t {
            return m.into_iter().map(|v| Vector(v)).collect();
        } else {
            return Vec::new();
        }
   }
}

m in Matrix(m) is not Vec<Vec<f64>>, it is type Mat

struct Mat {
 d: Vec<Vec<f64>>,
 row: usize,
 col: usize,
}

Mat do not implement trait Copy

I have also updated the origianl problem, thanks.

In that case your the error is not the real one since it says m has type Vec<Vec<f64>>. Edit: looks like you implemented Deref for Mat. You can't call methods that consume it through Deref, you need to call .into_iter() directly on m.d:

struct Mat {
 d: Vec<Vec<f64>>,
 row: usize,
 col: usize,
}
struct Vector(Vec<f64>);
enum CompoundType {
   Matrix(Mat),
   Other
}
impl From<CompoundType> for Vec<Vector> {
   fn from(t: CompoundType) -> Self {
        if let CompoundType::Matrix(m) = t {
            return m.d.into_iter().map(|v| Vector(v)).collect();
        } else {
            return Vec::new();
        }
   }
}
1 Like

Thanks, I see.
the cause that error says Vec<Vec<f64>> should be that I implement Deref for Mat

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.