Hi everyone, I tried to find an answer to my very easy question but couldn't find any.
I want to move an object that cannot be copied, out of other object, to be used in some other place. This is my code:
pub struct SomeResource { ... }
pub struct AllResources {
some: Option<SomeResource>
}
impl AllResources {
pub fn move_resource(&mut self) -> Option<SomeResource> {
// mem::replace(&mut self.some, None)
let some = self.some;
self.some = None;
some
}
}
This code example produces this compilation error:
cannot move out of
self.some
which is behind a mutable reference [E0507]
And everything compiles and works if I use the commented line instead. I don't see any problem in using the commented line, my question is - what is the right way to do this thing?
I saw some post here claiming that borrowing checker can recognize this pattern, but it doesn't recognize.