rust program a vec of vec of struct then find all struct appear in inside vec
- User
rust program a vec of vec of struct then find all struct appear in inside vec
- Assistant
You can create a vector of vectors of structs in Rust using the
vec!macro. To find all the structs that appear inside the inner vectors, you can iterate over the outer vector and inner vector using nested loops.Here's an example:
#[derive(Debug)] struct MyStruct { value: i32, } fn main() { let vec_of_vecs: Vec<Vec<MyStruct>> = vec![ vec![ MyStruct { value: 1 }, MyStruct { value: 2 }, MyStruct { value: 3 }, ], vec![ MyStruct { value: 4 }, MyStruct { value: 5 }, MyStruct { value: 6 }, ], vec![ MyStruct { value: 7 }, MyStruct { value: 8 }, MyStruct { value: 9 }, ], ]; let mut found_structs: Vec<MyStruct> = Vec::new(); for inner_vec in &vec_of_vecs { for item in inner_vec { found_structs.push(*item); // or push_ref(item) for references instead of copying } } println!("Found structs: {:?}", found_structs); }