我試圖應用一些面向對象,但我面臨一個問題。找不到方法/字段名稱
use std::io::Read;
struct Source {
look: char
}
impl Source {
fn new() {
Source {look: '\0'};
}
fn get_char(&mut self) {
self.look = 'a';
}
}
fn main() {
let src = Source::new();
src.get_char();
println!("{}", src.look);
}
編譯器報告這些錯誤,爲src.get_char();
:
error: no method named
get_char
found for type()
in the current scope
和println!("{}", src.look);
:
attempted access of field
look
on type()
, but no field with that name was found
我無法找出什麼我已經錯過了。
..但我認爲鏽有_return類型推斷_因爲許多新的語言有。爲什麼它不能從我寫的行中推斷'new'的返回類型? – deepmax
@deepmax:Rust在函數*中有類型推斷*,但要求函數簽名是明確的。但是,即使它確實如此,也會得出結論:由於分號,「new」應返回「()」。 Source {look:'\ 0'}'的類型是'Source',但Source {look:'\ 0'};'(帶分號)的類型是'()'。 –