|
|
|
|
|
by shepmaster
3330 days ago
|
|
Could you provide an example of such code? I was under the impression that certain things were disallowed because destructors aren't allowed to see the struct in an invalid state. A common case where I see people trying to do this is when you have a struct where you are trying to replace a member variable: struct Foo {
thing: Vec<i32>,
}
impl Foo {
fn something(&mut self) -> Vec<i32> {
let temp = self.thing;
// If we panicked between these two lines, then the struct would be in an undefined state
self.thing = vec![1];
temp
}
}
This code produces the error `cannot move out of borrowed content`. For those curious, you normally would write this as use std::mem;
impl Foo {
fn something(&mut self) -> Vec<i32> {
mem::replace(&mut self.thing, vec![1])
}
}
|
|