pub struct Character {
name: String,
hp: i32,
level: i32,
xp: i32,
xp_needed: i32,
gold: i32
}
impl Character {
pub fn new(name: String) -> Character {
let mut rng = thread_rng();
let hp: i32 = rng.gen_range(12, 75);
let gold: i32 = rng.gen_range(10, 50);
Character { name: name, hp: hp, level: 1, xp: 0, gold: gold, xp_needed: 100 }
}
pub fn get_name(&self) -> String {
self.name
}
// ...
}
How exactly am I breaking the rules here?
At a high-level, this is against-the-grain for Rust. You cannot transfer ownership of something borrowed because you don't own it.
Um don't I? I have other functions like:
pub fn get_hp(&self) -> i32 {
self.hp
}
And that works just fine.
|
23 | self.name
| ^^^^ cannot move out of borrowed content
error: aborting due to previous error
What's going on? What is the appropriate approach to return the character name? Why does the get_hp
method work but not get_name
?