rust
#[derive(Debug)]
pub struct Widget {
id: String,
children: Vec<Widget>
}
fn main() {
let mut flattened = vec![];
let tree = vec![
Widget {
id: "l1a".to_string(),
children: vec![
Widget {
id: "l2a".to_string(),
children: vec![
Widget {
id: "l3a".to_string(),
children: vec![]
},
Widget {
id: "l3b".to_string(),
children: vec![]
}
]
},
Widget {
id: "l2b".to_string(),
children: vec![]
}
]
},
Widget {
id: "l1b".to_string(),
children: vec![]
}
];
flattener(&tree, &mut flattened);
dbg!(flattened);
}
pub fn flattener(items: &Vec<Widget>, dest: &mut Vec<String>) {
items.iter().for_each(|item|
{
dest.push(item.id.to_string());
flattener(&item.children, dest);
}
);
}
results start
There's probably a better way to do this and I wouldn't
be surprised to learn there are bugs with this
approach, but this is working for me