2012-03-11 3 views
0

간단한 녹 프로그램을 작성합니다.녹 오류 "이 표현식의 유형을 결정할 수 없습니다"

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 

어떻게 된 것입니까?

답변

4

컴파일러가이 호출을 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 
    }; 
} 

유형의 추론이 가끔 문 작은 시리즈로 분해 식에 도움이 될 수와 종류가 일치하지 않는 위치를 알아낼 때까지 명시 적 유형을 삽입 할 수 있습니다 실패하면 바르게. 나는 이런 식으로 뭔가를 쳐서 잠시 동안 그것을 째려하여 알아낼 수없는 경우, 그때 유형 connectsend 실제로 사용하는 어떤 코스 치환

fn main(){ 
    let port = 80; 
    let conn_result: result::t<connection, str> = connect("localhost", port as u16); 
    let send_fn = [email protected](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); 
} 

처럼 다시 쓰기 시작한다. 모든 것을 따로 떼어내는 과정에서 당신과 컴파일러가 동의하지 않는 부분을 발견하게 될 것입니다.

관련 문제