Rust 运算符简化错误处理

Beginner

This tutorial is from open-source community. Access the source code

简介

在本实验中,引入了 ? 运算符,作为一种在链式结果时使代码更简洁的方法。它用于返回 Result 的表达式末尾,并通过自动处理 ErrOk 分支来简化代码。提供的示例演示了如何在 Rust 中使用 ? 运算符来处理各种数学运算及其潜在错误。

注意:如果实验未指定文件名,你可以使用任何你想要的文件名。例如,你可以使用 main.rs,并通过 rustc main.rs &&./main 进行编译和运行。

?

使用 match 来链式处理结果可能会变得相当杂乱;幸运的是,可以使用 ? 运算符来让事情再次变得整洁。? 用于返回 Result 的表达式末尾,它等同于一个 match 表达式,其中 Err(err) 分支展开为提前返回 Err(From::from(err)),而 Ok(ok) 分支展开为 ok 表达式。

mod checked {
    #[derive(Debug)]
    enum MathError {
        DivisionByZero,
        NonPositiveLogarithm,
        NegativeSquareRoot,
    }

    type MathResult = Result<f64, MathError>;

    fn div(x: f64, y: f64) -> MathResult {
        if y == 0.0 {
            Err(MathError::DivisionByZero)
        } else {
            Ok(x / y)
        }
    }

    fn sqrt(x: f64) -> MathResult {
        if x < 0.0 {
            Err(MathError::NegativeSquareRoot)
        } else {
            Ok(x.sqrt())
        }
    }

    fn ln(x: f64) -> MathResult {
        if x <= 0.0 {
            Err(MathError::NonPositiveLogarithm)
        } else {
            Ok(x.ln())
        }
    }

    // 中间函数
    fn op_(x: f64, y: f64) -> MathResult {
        // 如果 `div` “失败”,那么将返回 `DivisionByZero`
        let ratio = div(x, y)?;

        // 如果 `ln` “失败”,那么将返回 `NonPositiveLogarithm`
        let ln = ln(ratio)?;

        sqrt(ln)
    }

    pub fn op(x: f64, y: f64) {
        match op_(x, y) {
            Err(why) => panic!("{}", match why {
                MathError::NonPositiveLogarithm
                    => "logarithm of non-positive number",
                MathError::DivisionByZero
                    => "division by zero",
                MathError::NegativeSquareRoot
                    => "square root of negative number",
            }),
            Ok(value) => println!("{}", value),
        }
    }
}

fn main() {
    checked::op(1.0, 10.0);
}

一定要查看文档,因为有许多方法可以对 Result 进行映射/组合。

总结

恭喜你!你已经完成了 ? 实验。你可以在 LabEx 中练习更多实验来提升你的技能。