私はいくつかのOOPを適用しようとしていますが、私は問題に直面しています。メソッド/フィールド名が見つかりません
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
私が見逃しているものを見つけることができません。
..しかし、多くの新しい言語があるので、Rustには_return型の推論があると思っています。なぜ私が書いた行から 'new'の戻り値の型を推論しないのでしょうか? – deepmax
@deepmax:Rustは関数*内で型推論*を持っていますが、関数シグネチャを明示的に指定する必要があります。しかし、それがあったとしても、 'new'はセミコロンのために'() 'を返すべきだと結論づけます。 'Source {look: '\ 0'}'の型は 'Source'ですが、' Source {look: '\ 0'}; '(セミコロン付き)の型は'() 'です。 –