pub type Data = i32;
pub struct Foo {
data: Data,
}
impl Foo {
pub fn data_mut(&mut self) -> &mut Data {
&mut self.data
}
}
pub
With
pub fn broken(&mut self) -> &mut Data {
&mut self.foos.first_mut().unwrap().data_mut()
}
the core issue is that the return type of data_mut()
is already a &mut Data
value, so you're essentially creating a &mut &mut Data
, though that will collapse. The simplest fix in your case is to drop the &mut
entirely
pub fn broken(&mut self) -> &mut Data {
self.foos.first_mut().unwrap().data_mut()
}
It would seem that by adding the &mut
you're causing the borrow checker to create a temporary location and then take a reference to that location.