Hello,
This pattern is safe but the stable borrow checker doesn't agree. Polonius is cool with it though.
fn func(vec: &mut Vec<usize>) -> Option<&mut usize> {
for i in 0..1000 {
//Needs Polonius to work
match vec.get_mut(i) {
Some(result) => {return Some(result);},
None => {}
};
//Works reagrdless
// let mut idx = None;
// match vec.get_mut(i) {
// Some(result) => {idx = Some(i)},
// None => {}
// };
// match idx {
// Some(idx) => {return vec.get_mut(idx)},
// None => {}
// }
}
None
}
fn main() {
let mut my_vec = vec![9, 8, 7];
let result = func(&mut my_vec);
println!("{result:?}");
}
I have one workaround (commented out) but it's not workable in practice. (Vec::get_mut
is a placeholder for another method with the same kind of signature)
Does anyone know another work-around that's available on stable that doesn't require calling the method twice?
Thank you