pub enum MyEnum{
Int(i32),
Float(f32)
}
fn convert(my_vec:Vec<i32>) -> Vec<MyEnum>
{
//??
}
I need to convert a vector of ints to a vector of enum type Int.
pub enum MyEnum{
Int(i32),
Float(f32)
}
fn convert(my_vec:Vec<i32>) -> Vec<MyEnum>
{
//??
}
I need to convert a vector of ints to a vector of enum type Int.
A simple map()
+ collect()
will do:
my_vec.iter().map(|v| MyEnum::Int(*v)).collect::<Vec<_>>()
// Or
my_vec.iter().copied().map(MyEnum::Int).collect::<Vec<_>>()