Rust 错误处理——捕获多个错误

Rust error handling - capturing multiple errors

我上周开始学习 Rust,通过阅读书籍和文章,同时尝试从其他语言转换一些代码。

我遇到了一种情况,我试图通过下面的代码来举例说明(这是我试图从另一种语言转换的简化版本):

#[derive(Debug)]
struct InvalidStringSize;
impl std::fmt::Display for InvalidStringSize {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "string is too short")
    }
}
impl std::error::Error for InvalidStringSize {}

pub fn extract_codes_as_ints(
    message: String,
) -> Result<(i32, i32, i32), Box<dyn std::error::Error>> {
    if message.len() < 20 {
        return Err(Box::new(InvalidStringSize {}));
    }
    let code1: i32 = message[0..3].trim().parse()?;
    let code2: i32 = message[9..14].trim().parse()?;
    let code3: i32 = message[17..20].trim().parse()?;
    Ok((code1, code2, code3))
}

所以基本上我想从给定字符串的特定位置提取 3 个整数(我也可以尝试检查其他字符的某些模式,但我忽略了那部分)。

我想知道,有没有办法同时“捕获”或验证解析调用的所有三个结果?我不想为每个都添加一个匹配块,我只想检查是否有人导致错误,并且 return 在这种情况下会出现另一个错误。有道理吗?

到目前为止我能想到的唯一解决方案是创建另一个包含所有解析的函数,并匹配其结果。还有其他方法吗?

此外,非常欢迎代码其他部分的任何 feedback/suggestions,我正在努力寻找在 Rust 中做事的“正确方法”。

完成此操作的惯用方法是定义您自己的错误类型和 return 它,并为您的函数中可能出现的每种错误类型 T 提供 From<T> 实现。 ? 运算符将进行 .into() 转换以匹配您的函数声明为 return.

的错误类型

这里的盒装错误太过分了;只需声明一个枚举,列出函数可能失败的所有方式。整数解析错误的变体甚至可以捕获捕获的错误。

use std::fmt::{Display, Formatter, Error as FmtError};
use std::error::Error;
use std::num::ParseIntError;

#[derive(Debug, Clone)]
pub enum ExtractCodeError {
    InvalidStringSize,
    InvalidInteger(ParseIntError),
}

impl From<ParseIntError> for ExtractCodeError {
    fn from(e: ParseIntError) -> Self {
        Self::InvalidInteger(e)
    }
}

impl Error for ExtractCodeError {}

impl Display for ExtractCodeError {
    fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
        match self {
            Self::InvalidStringSize => write!(f, "string is too short"),
            Self::InvalidInteger(e) => write!(f, "invalid integer: {}", e)
        }
    }
}

现在我们只需要更改函数的 return 类型,并在长度太短时将其设为 return ExtractCodeError::InvalidStringSize。无需更改任何其他内容,因为 ParseIntError 会自动转换为 ExtractCodeError:

pub fn extract_codes_as_ints(
    message: String,
) -> Result<(i32, i32, i32), ExtractCodeError> {
    if message.len() < 20 {
        return Err(ExtractCodeError::InvalidStringSize);
    }
    let code1: i32 = message[0..3].trim().parse()?;
    let code2: i32 = message[9..14].trim().parse()?;
    let code3: i32 = message[17..20].trim().parse()?;
    Ok((code1, code2, code3))
}

作为一个额外的好处,与使用盒装 dyn Error 相比,此函数的调用者将能够更容易地检查错误。

在更复杂的情况下,例如您希望针对每个可能出现的 ParseIntError 稍微调整错误,您可以对结果使用 .map_err() 来转换错误。例如:

something_that_can_fail.map_err(|e| SomeOtherError::Foo(e))?;