try!
Chaining results using match can get pretty untidy; luckily, the try!
macro
can be used to make things pretty again. The try!
macro expands to a match
expression, where the Err(err)
branch expands to an early return Err(err)
,
and the Ok(ok)
branch expands to an ok
expression.
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()) } } // Intermediate function fn op_(x: f64, y: f64) -> MathResult { // if `div` "fails", then `DivisionByZero` will be `return`ed let ratio = try!(div(x, y)); // if `ln` "fails", then `NegativeLogarithm` will be `return`ed let ln = try!(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); }
Be sure to check the documentation,
as there are many methods to map/compose Result
.