Consider the following code:
struct NonCloneCopyableStruct; // Just a placeholder example.
pub struct TestStruct{
a_new: NonCloneCopyableStruct,
a_old: Option<NonCloneCopyableStruct>
}
impl TestStruct {
pub fn new(a_new: NonCloneCopyableStruct) -> Self {
Self { a_new , a_old: None }
}
fn update(&mut self, new: NonCloneCopyableStruct) {
self.a_old = Some(self.a_new);
self.a_new = new;
}
}
fn main() {
let mut test_struct = TestStruct::new(NonCloneCopyableStruct);
test_struct.update(NonCloneCopyableStruct);
}
It is technically valid, even if it it temporarily invalid as seen by the borrow checker.
error[E0507]: cannot move out of `self.a_new` which is behind a mutable reference
Am I required to use unsafe here to allow for this technicality?
And if so, how?