일부 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'};'(세미콜론과 함께)의 타입은'()'입니다. –