【发布时间】:2022-01-02 05:03:57
【问题描述】:
我正在学习 rust,目前正在练习 here。
到目前为止,我想出了以下几点:
impl ParsePosNonzeroError {
...
...
fn from_parseint(err: ParseIntError) -> ParsePosNonzeroError {
ParsePosNonzeroError::ParseInt(err)
}
fn parse_pos_nonzero(s: &str)
-> Result<PositiveNonzeroInteger, ParsePosNonzeroError>
{
let x: i64 = s.parse::<i64>().map_err(ParsePosNonzeroError::from_parseint);
PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
}
当我这样做时,我收到错误消息:
! Compiling of exercises/error_handling/errors6.rs failed! Please try again. Here's the output:
error[E0308]: mismatched types
--> exercises/error_handling/errors6.rs:34:18
|
34 | let x: i64 = s.parse::<i64>()
| ____________---___^
| | |
| | expected due to this
35 | | .map_err(ParsePosNonzeroError::from_parseint);
| |_____________________________________________________^ expected `i64`, found enum `Result`
|
= note: expected type `i64`
found enum `Result<i64, ParsePosNonzeroError>`
浏览map_err 的文档,我看到以下内容:
Maps a Result<T, E> to Result<T, F> by applying a function to a contained Err value, leaving an Ok value untouched.
我错过了什么?为什么这不起作用?
【问题讨论】:
标签: rust error-handling