我正在弄清楚为什么返回时以下失败map_err:
Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
--> src/main.rs:37:5
|
36 | fn R() -> Result<i32, MyError> {
| -------------------- expected `std::result::Result<i32, MyError>` because of return type
37 | decode().map_err(|e| Err(MyError {v: "changed".to_string() }))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `MyError`, found enum `std::result::Result`
|
= note: expected enum `std::result::Result<_, MyError>`
found enum `std::result::Result<_, std::result::Result<_, MyError>>`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground`.
Run Code Online (Sandbox Code Playgroud)
如果我match手动使用并映射它,它就可以工作。
代码:
Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
--> src/main.rs:37:5
|
36 | fn R() -> Result<i32, MyError> {
| -------------------- expected `std::result::Result<i32, MyError>` because of return type
37 | decode().map_err(|e| Err(MyError {v: "changed".to_string() }))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `MyError`, found enum `std::result::Result`
|
= note: expected enum `std::result::Result<_, MyError>`
found enum `std::result::Result<_, std::result::Result<_, MyError>>`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground`.
Run Code Online (Sandbox Code Playgroud)
如果您查看签名,map_err您会发现它需要一个接受错误类型并返回错误类型的闭包,而不是Result.
decode().map_err(|e| MyError {v: "changed".to_string() })
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
97 次 |
| 最近记录: |