This code works as expected in Rust 1.27.2:
struct X {
a: String,
b: String,
}
impl X {
fn func(mut self: Self) -> String {
let _a = self.a;
self.a = "".to_string();
self.b
}
}
fn main() {
let x = X {
a: "".to_string(),
b: "".to_string(),
};
x.func();
}
Using the same compiler version, the X
instance is now boxed. Even without trying to mutate the partial object, the borrow checker stops reasoning behind the self
:
struct X {
a: String,
b: String,
}
impl X {
fn func(self: Box<Self>) -> String {
let _a = self.a;
self.b
}
}
fn main() {
let x = Box::new(X {
a: "".to_string(),
b: "".to_string(),
});
x.func();
}
Error message:
error[E0382]: use of moved value: `self`
--> src/main.rs:9:9
|
8 | let _a = self.a;
| -- value moved here
9 | self.b
| ^^^^^^ value used here after move
|
= note: move occurs because `self.a` has type `std::string::String`, which does not implement the `Copy` trait
Is it by design that the boxed objects cannot be partially borrowed or is it a regression in the ongoing refactoring of the borrow checker?