Читаем Rust by Example полностью

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

<p><strong>?</strong></p>

Chaining results using match can get pretty untidy; luckily, the ? operator can be used to make things pretty again. ? is used at the end of an expression returning a Result, and is equivalent to a match expression, where the Err(err) branch expands to an early Err(From::from(err)), and the Ok(ok) branch expands to an ok expression.

mod checked {

#[derive(Debug)]

enum MathError {

DivisionByZero,

NonPositiveLogarithm,

NegativeSquareRoot,

}

type MathResult = Result;

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())

}

}

// Intermediate function

fn op_(x: f64, y: f64) -> MathResult {

// if `div` "fails", then `DivisionByZero` will be `return`ed

let ratio = div(x, y)?;

// if `ln` "fails", then `NonPositiveLogarithm` will be `return`ed

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

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Be sure to check the documentation, as there are many methods to map/compose Result.

<p id="panic_1"><strong><a l:href="#panic_1">panic!</a></strong></p>

The panic! macro can be used to generate a panic and start unwinding its stack. While unwinding, the runtime will take care of freeing all the resources owned by the thread by calling the destructor of all its objects.

Since we are dealing with programs with only one thread, panic! will cause the program to report the panic message and exit.

// Re-implementation of integer division (/)

Перейти на страницу:

Похожие книги