我是 Rust 的新手,所以仍在尝试习惯该语言的内存模型。
cannot move out of borrowed content.
因此,当我在结构上构建getter
方法时遇到错误。我不太明白为什么会这样,但它似乎与枚举的某些特征有关。
enum Gender{
Male,
Female,
}
impl Default for Gender {
fn default() -> Gender { Gender::Female }
}
impl Clone for Gender {
fn clone(&self) -> Gender { *self }
}
#[derive(Default, Builder, Debug)]
#[builder(setter(into))]
struct ProfessorGroup {
name: &'static str,
gender:Gender,
level:Levels,
attrition_rate:f64,
promotion_rate:f64,
hiring_rate:f64,
current_number:u32,
}
impl ProfessorGroup {
pub fn get_gender(&self) -> Gender { self.gender }
pub fn get_name(&self) -> &'static str {self.name}
pub fn get_attrition_rate(&self) -> f64 {self.attrition_rate}
我收到的错误消息与Enum
性别有关。
error[E0507]: cannot move out of borrowed content
--> src/Actors/ProfessorGroups.rs:42:33
42 | pub fn get_gender(&self) -> Gender { self.gender }
| ^^^^ cannot move out of borrowed content
error[E0507]: cannot move out of borrowed content
--> src/Actors/ProfessorGroups.rs:27:33
|
27 | fn clone(&self) -> Levels { *self }
| ^^^^^ cannot move out of borrowed content
我确保参考自我。我也没有在这些函数中进行任何分配——只是 getter 函数——但不知何故,借用变得一团糟。