I'm writing a very simple getter/setting model that I would like to start using in Rust for simplicity reasons using struct
and impl
.
struct Person {
firstName: String,
lastName: String,
}
impl Person {
fn get_first_name(&mut self) -> String { return self.firstName; }
fn get_last_name(&mut self) -> String { return self.lastName; }
fn set_first_name(&mut self, x: String) { self.firstName = x; }
fn set_last_name(&mut self, x: String) { self.lastName = x; }
fn default() -> Person {
Person {firstName: "".to_string(), lastName: "".to_string()}
}
}
fn main() {
let mut my_person : Person = Person{ ..Person::default() };
my_person.set_first_name("John".to_string());
my_person.set_last_name("Doe".to_string());
println!("{}", my_person.firstName);
println!("{}", my_person.lastName);
}
When I run this snippet I get the following error.
src\main.rs:7:53: 7:57 error: cannot move out of borrowed content [E0507]
src\main.rs:7 fn get_first_name(&mut self) -> String { return self.firstName; }
^~~~
src\main.rs:8:53: 8:57 error: cannot move out of borrowed content [E0507]
src\main.rs:8 fn get_last_name(&mut self) -> String { return self.lastName; }
^~~~
error: aborting due to 2 previous errors
Could not compile `sandbox`.
Can someone point out the mistake to me since I'm very new to Rust?
Tips on writing this snippet better would be accepted too. I'm always looking for easier/faster readability.