Consider the following code:
struct Data {
id: i32,
}
struct Wrapper {
datas: Vec<Data>,
}
impl Wrapper{
fn insert_after(&mut self, id: i32, new_data: Data) {
let mut index = None;
for (i, data) in self.datas.iter().enumerate() {
if data.id == id {
index = Some(i);
break;
}
}
if let Some(index) = index {
self.datas.insert(index, new_data);
}
}
}
Is there a way to achieve the same functionality as insert_after using only iterators and no for loop? Is there a way to remove the additional if statement, and the optional index variable?