rebel 在 学rust的疑问,为什么有这种莫名其妙的语法 中发帖
日常使用js和java
看到这种写法真是难绷:add(&mut self, x: i32) → &mut Self
// 结构体(类似 JavaScript 的类)
pub struct Calculator {
result: i32,
}
impl Calculator {
// 构造函数
pub fn new() -> Self {
Calculator { result: 0 }
}
pub fn add(&mut self, x: i32) -> &mut Self {
self.result += x;
self
}
pub fn get_result(&self) -> i32 {
self.result
}
}