我试图应用一些面向对象,但我面临一个问题。找不到方法/字段名称
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'};'(带分号)的类型是'()'。 –