#[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.)
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.
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?
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();
}
}
}