I have a struct containing a mutable reference and an owned value. I'm trying to see if I can implement some getters for these values. To my surprise, getx compiles but gety doesn't. Given that neither &mut String nor String implements Copy, why would the compiler allow getting a &mut String but not a String?
struct S<'a> {
x: &'a mut String,
y: String,
}
impl<'a> S<'a> {
fn getx(&mut self) -> &mut String {
self.x // Ok
}
fn gety(&mut self) -> String {
self.y // Err
}
}
Error messages:
error[E0507]: cannot move out of
self.ywhich is behind a mutable reference
move occurs because
self.yhas typeString, which does not implement theCopytrait