문제

나는 간단한 녹 프로그램을 쓴다.

fn main(){
    let port = 80;
    result::chain(connect("localhost", port as u16)) {|s|
    send(s,str::bytes("hello world"));
};
.

그것이 몇 가지 오류가 있습니다.

macmatoMacBook-Air-2:rust-http kula$ rustc http.rs
http.rs:40:4: 40:52 error: cannot determine a type for this expression
http.rs:40     result::chain(connect("localhost", port as u16)) {|s|
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous errors
.

어떻게 된거 야?

도움이 되었습니까?

해결책

컴파일러는 이러한 result::chain의 호출이 반환되도록 해당하는 유형을 추론하지 못했습니다. connectsend의 유형을 알지 못하지 않고 확실하지만, 람다 블록의 본문이 (아마 실수로)이므로 nil 형을 가져 오기 때문입니다.

녹의 모든 블록의 유형은 '꼬리 표현식'에 의해 결정되며, 꼬리 표현은 최종 성명서의 세미콜론을 떠나는 것으로 생성됩니다. 아마도 sendresult 유형을 반환하고 result::chain를 사용하여 전체 표현식의 결과가 send의 결과입니다. 이 작업을 수행하려면 send 표현식을 세미콜론으로 종료해서는 안됩니다. 그런 다음 람다 블록은 send의 결과를 반환합니다.

이와 같은 것이 더 잘 작동 할 수 있습니다 :

fn main(){
    let port = 80;
    result::chain(connect("localhost", port as u16)) {|s|
        send(s,str::bytes("hello world")) // <- no semicolon
    };
}
.

유형 추론이 실패하면 때로는 표현식을 더 작은 일련의 명령문으로 분해하고 유형이 올바르게 일치하지 않는 위치를 파악할 때까지 명시 적 유형을 삽입하는 경우가 있습니다. 내가 이렇게 쳤고 잠시 동안 그것을 안경으로 알아낼 수 없으면 처럼 다시 작성을 시작할 것입니다.

fn main(){
    let port = 80;
    let conn_result: result::t<connection, str> = connect("localhost", port as u16);
    let send_fn =  fn@(s: connection) -> result::t<str, str> {
        let send_result: result<str, str> = send(s,str::bytes("hello world"));
        ret send_result;
    };
    let res: result<str, str> = result::chain(conn_result, send_fn);
}
.

of typement connectsend가 실제로 사용하는 유형을 대체합니다. 모든 것을 잡아 당기는 과정에서 당신은 당신과 컴파일러가 동의하지 않는 곳을 알게 될 것입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top