I know there is something wrong, but I can not figure out how to solve this.
I have a vec of students over which I iterate. If any student is the only applicant for a gift (he/she wants to have gift x) they get assign to that gift. So in my mind I need those nested iterations with and also the mutable borrows. How can I solve this in an ideomatic way?
pub fn solve(&mut self) {
for student in self.students.iter_mut() {
let is_only_applicant_for_primary = !self.students.iter().any(|s| s.primary == s.primary && s != student);
if is_only_applicant_for_primary {
println!("Student {} is the only applicant for gift nr.{}", student.nr, student.primary);
self.assign(student, student.primary);
}
}
}
fn assign(&mut self, student: &Student, gift_nr: u16) {
let gift = self.gifts.remove((gift_nr - 1).into());
self.assignments.push(Assignment::new(student.to_owned(), gift));
self.students.retain(|s| s != student);
}
PS: I am fairly new to Rust so please do not hesitate to correct me on anything I might have not understood.