If one of the elements in an enum is a vector, how can data be pushed into the vector?
For example:
struct T{
p:i32
}
pub enum Foo{
foo_vec:Vec<T>,
generic_data:i32
}
fn main(){
let mut m=Foo(foo_vec::new()
}
How do I insert data into the foo_vec here?
Your enum isn't an enum. You are trying to use struct syntax to define an enum. Please read the TRPL, specifically the section on enums
I think you meant
pub enum Foo{
Bar {
foo_vec:Vec<T>,
generic_data:i32
}
}
In which case, you can create it like so,
Foo::Bar { foo_vec: Vec::new(), generic_data: 0 }
Or modify fields like so,
if let Foo::Bar { foo_vec, generic_data } = &mut foo {
foo_vec.push(value)
}
system
Closed
4
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.