I have a struct
struct Foo {
foo1: String,
foo2: String,
foo3: String,
foo4: String,
// ...
}
I would like to create an instance of Foo
from a vector.
let x = vec!["a".to_string(), "b".to_string(), "c".to_string(), "d".to_string()];
match x.as_slice() {
&[ref a, ref b, ref c, ref d] => {
let foo = Foo {
foo1: a.to_string(),
foo2: b.to_string(),
foo3: c.to_string(),
foo4: d.to_string(),
};
},
_ => unreachable!(),
}
Do I have to copy the strings? Is there any better way to destructure the vector into a
, b
, c
, d
as well as transferring the ownership?
Actually, I don't mind x
is completely destroyed after the destructuring. So I hope there is a pattern match for vectors apart from slices as well. For now it seems we can only destructure slices.