How does Rust infer resultant types from From::<>::from()?

后端 未结 2 608
深忆病人
深忆病人 2021-01-27 20:32

In this snippet from Hyper\'s example, there\'s a bit of code that I\'ve annotated with types that compiles successfully:

.map_err(|x: std::io::Error| -> hype         


        
2条回答
  •  孤城傲影
    2021-01-27 20:54

    Type information in Rust can flow backwards.

    The return type of the closure is specified to be hyper::Error. Therefore, the result of the block must be hyper::Error, therefore the result of From::from must be hyper::Error.

    If you wanted to, you could use ...

    ::::from(x)
    

    ... which would be the even more fully qualified version. But with the closure return type there, it's unnecessary.

提交回复
热议问题