17
我在擺弄Rust,試着做例子,試着做一個課。我一直在尋找的example of StatusLineText
Rust中的對象和類
它不斷提高的錯誤:
error: `self` is not available in a static method. Maybe a `self` argument is missing? [E0424]
self.id + self.extra
^~~~
error: no method named `get_total` found for type `main::Thing` in the current scope
println!("the thing's total is {}", my_thing.get_total());
^~~~~~~~~
我的代碼是相當簡單:
fn main() {
struct Thing {
id: i8,
extra: i8,
}
impl Thing {
pub fn new() -> Thing {
Thing { id: 3, extra: 2 }
}
pub fn get_total() -> i8 {
self.id + self.extra
}
}
let my_thing = Thing::new();
println!("the thing's total is {}", my_thing.get_total());
}
爲了澄清,現在必須在方法的參數中明確地聲明自Rust 0.6 – 2013-06-26 18:29:21