使用 Box 将错误装箱 - Rust By Example
Description: Rust by Example (RBE) is a collection of runnable examples that illustrate various Rust concepts and standard libraries.

## Keyboard shortcuts

Press `←` or `→` to navigate between chapters

Press `S` or `/` to search in the book

Press `?` to show this help

Press `Esc` to hide this help

# 使用 `Box` 将错误装箱

一种既能编写简洁代码又能保留原始错误信息的方法是使用 `Box` 将它们装箱。这种方法的缺点是底层错误类型只能在运行时确定，而不是静态确定的。

标准库通过让 `Box` 实现从任何实现了 `Error` trait 的类型到 trait 对象 `Box<Error>` 的转换来帮助我们装箱错误，这是通过 `From` 实现的。

    use std::error;
    use std::fmt;

    // 将别名改为使用 `Box<dyn error::Error>`。
    type Result<T> = std::result::Result<T, Box<dyn error::Error>>;

    #[derive(Debug, Clone)]
    struct EmptyVec;

    impl fmt::Display for EmptyVec {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "无效的第一个待加倍项")
        }
    }

    impl error::Error for EmptyVec {}

    fn double_first(vec: Vec<&str>) -> Result<i32> {
        vec.first()
            .ok_or_else(|| EmptyVec.into()) // Converts to Box using Into trait.
            .and_then(|s| {
                s.parse::<i32>()
                    .map_err(From::from) // Converts to Box using From::from fn pointer.
                    .map(|i| 2 * i)
            })
    }

    fn print(result: Result<i32>) {
        match result {
            Ok(n) => println!("第一个数的两倍是 {}", n),
            Err(e) => println!("错误：{}", e),
        }
    }

    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));
    }

### 另请参阅：

动态分发和 `Error` trait