将错误装箱还有一种替代方案是将你自己的错误类型打包成枚举类型。

use std::error;

use std::error::Error;

use std::num::ParseIntError;

use std::fmt;


type Result<T> = std::result::Result<T, DoubleError>;


#[derive(Debug)]

enum DoubleError {

    EmptyVec,

    // We will defer to the parse error implementation for their error.

    // Supplying extra info requires adding more data to the type.

    Parse(ParseIntError),

}


impl fmt::Display for DoubleError {

    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

        match *self {

            DoubleError::EmptyVec =>

                write!(f, "please use a vector with at least one element"),

            // The wrapped error contains additional information and is available

            // via the source() method.

            DoubleError::Parse(..) =>

                write!(f, "the provided string could not be parsed as int"),

        }

    }

}


impl error::Error for DoubleError {

    fn source(&self) -> Option<&(dyn error::Error + 'static)> {

        match *self {

            DoubleError::EmptyVec => None,

            // The cause is the underlying implementation error type. Is implicitly

            // cast to the trait object `&error::Error`. This works because the

            // underlying type already implements the `Error` trait.

            DoubleError::Parse(ref e) => Some(e),

        }

    }

}


// Implement the conversion from `ParseIntError` to `DoubleError`.

// This will be automatically called by `?` if a `ParseIntError`

// needs to be converted into a `DoubleError`.

impl From<ParseIntError> for DoubleError {

    fn from(err: ParseIntError) -> DoubleError {

        DoubleError::Parse(err)

    }

}


fn double_first(vec: Vec<&str>) -> Result<i32> {

    let first = vec.first().ok_or(DoubleError::EmptyVec)?;

    // Here we implicitly use the `ParseIntError` implementation of `From` (which

    // we defined above) in order to create a `DoubleError`.

    let parsed = first.parse::<i32>()?;


    Ok(2 * parsed)

}


fn print(result: Result<i32>) {

    match result {

        Ok(n)  => println!("The first doubled is {}", n),

        Err(e) => {

            println!("Error: {}", e);

            if let Some(source) = e.source() {

                println!("  Caused by: {}", source);

            }

        },

    }

}


fn main() {

    let numbers = vec!["42", "93", "18"];

    let empty = vec![];

    let strings = vec!["tofu", "93", "18"];


    print(double_first(numbers));

    print(double_first(empty));

    print(double_first(strings));

}

这会给你的错误处理程序增加一点样板文件,它也许不是所用应用程序都需要的。有一些库会为你处理这些样板文件。

内容解析

这种方法通过定义自己的错误枚举类型来包装不同类型的错误,而不是使用Box<dyn Error>。

代码解析

错误类型定义:

#[derive(Debug)]

enum DoubleError {

    EmptyVec,                    // 空向量错误

    Parse(ParseIntError),        // 包装ParseIntError,保留原始错误信息

}

display接口的实现

impl fmt::Display for DoubleError {

    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

        match *self {

            DoubleError::EmptyVec =>

                write!(f, "please use a vector with at least one element"),

            DoubleError::Parse(..) =>

                write!(f, "the provided string could not be parsed as int"),

        }

    }

}

Error 接口的实现(关键部分)

impl error::Error for DoubleError {

    fn source(&self) -> Option<&(dyn error::Error + 'static)> {

        match *self {

            DoubleError::EmptyVec => None,

            DoubleError::Parse(ref e) => Some(e), // 提供底层错误源

        }

    }

}

From接口的实现

impl From<ParseIntError> for DoubleError {

    fn from(err: ParseIntError) -> DoubleError {

        DoubleError::Parse(err) // 将ParseIntError转换为DoubleError

    }

}

主函数:

fn double_first(vec: Vec<&str>) -> Result<i32> {

    let first = vec.first().ok_or(DoubleError::EmptyVec)?; // 手动创建EmptyVec错误

    let parsed = first.parse::<i32>()?; // 自动转换:ParseIntError → DoubleError

    Ok(2 * parsed)

}

改进的错误打印

fn print(result: Result<i32>) {

    match result {

        Ok(n)  => println!("The first doubled is {}", n),

        Err(e) => {

            println!("Error: {}", e);

            if let Some(source) = e.source() { // 使用source方法获取底层错误

                println!("  Caused by: {}", source);

            }

        },

    }

}

运行结果分析

fn main() {

    let numbers = vec!["42", "93", "18"];

    let empty = vec![];

    let strings = vec!["tofu", "93", "18"];


    print(double_first(numbers));

    // 输出:The first doubled is 84


    print(double_first(empty));

    // 输出:

    // Error: please use a vector with at least one element

    // (没有底层原因,因为EmptyVec没有source)


    print(double_first(strings));

    // 输出:

    // Error: the provided string could not be parsed as int

    //   Caused by: invalid digit found in string

    // (显示了自定义错误消息和底层ParseIntError的详细信息)

}
优点:

类型安全:编译时就知道所有可能的错误类型

完整的错误信息:提供了友好的用户消息,又保留了底层错误的详细信息

结构化错误处理:错误类型明确,便于模式匹配和处理

可扩展性:可以轻松添加新的错误变体

更好的API设计:对于库开发,提供具体的错误类型比trait对象更友好

缺点:

样板文件:需要实现多个trait(Display、Error、From等)

开发成本:需要为每个错误类型手动定义转换和处理逻辑

和之前的方法对比

特性

自定义错误枚举

错误装箱

类型安全

编译时已知

运行时才知道

错误信息

结构化和完整

保留原始信息

代码量

较多样板代码

简洁

性能

无堆分配

需要堆分配

扩展性

需要修改枚举

自动支持新错误类型

适用场景

库开发、需要精确错误处理

应用程序开发、快速原型

总结

这段代码展示了Rust错误处理的另一种高级模式:使用自定义错误枚举来包装不同类型的错误。

核心价值

提供了类型安全的错误处理,所有可能的错误变体在编译时都是已知的

既能为最终用户提供友好的错误信息,又能为开发者保留详细的错误信息

通过From接口实现和?操作符的配合,实现了优雅的错误传播

通过source()方法提供了错误链追踪的能力

适用场景

库开发:为库使用者提供明确的错误类型和文档

生成环境:需要精确控制和报告错误信息的应用程序

复杂错误处理:需要根据不同错误类型采取不同处理策略的场景

生态系统支持

如代码注释中提到的:有一些库(如thiserror、anyhow、snafu等)可以大大减少这些样板代码的编写工作量,同时保持这种模式的优点。

这种模式代表了Rust错误处理的最佳实践之一,特别适合需要高质量错误处理的严肃项目。

原文链接地址:Wrapping errors - Rust By Example

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐