fn main() {
struct Person {
name: String
}
impl Person {
fn get_name(&self) -> String {
self.name
}
}
let me = Person {
name: String::from("Manuel")
};
println!("{}", me.get_name());
}
Given this code the compiler returns the error
self.name | ^^^^^^^^^ move occurs because
self.namehas typeString, which does not implement theCopytrait
So i fixed it with
fn get_name(&self) -> &String {
&self.name
}
But i don't get why? Given that I already defined the argument as &self, why I have to specify the & operator also to the return type and value? In my head, the &self already means "whatever is passed as argument, it is already a borrow of the pointer"
I'm a bit confused