?
?
使用匹配链接结果会得到极其繁琐的内容;幸运的是,? 运算符可以使事情再次变得干净漂亮。? 运算符用在返回值为 Result 的表式式后面,等同于这样一个匹配表式,其中 Err(err) 分支展开成提前(返回)return Err(err),同时 Ok(ok) 分支展开成 ok 表达式。
mod checked {#[derive(Debug)]enum MathError {DivisionByZero,NegativeLogarithm,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::NegativeLogarithm)} else {Ok(x.ln())}}// 中间函数fn op_(x: f64, y: f64) -> MathResult {// 如果 `div` “失败”了,那么 `DivisionByZero` 将被返回let ratio = div(x, y)?;// 如果 `ln` “失败”了,那么 `NegativeLogarithm` 将被返回let ln = ln(ratio)?;sqrt(ln)}pub fn op(x: f64, y: f64) {match op_(x, y) {Err(why) => panic!(match why {MathError::NegativeLogarithm=> "logarithm of negative 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。
