Выбрать главу

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.

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

fn division(dividend: i32, divisor: i32) -> i32 {

if divisor == 0 {

// Division by zero triggers a panic

panic!("division by zero");

} else {

dividend / divisor

}

}

// The `main` task

fn main() {

// Heap allocated integer

let _x = Box::new(0i32);

// This operation will trigger a task failure

division(3, 0);

println!("This point won't be reached!");

// `_x` should get destroyed at this point

}

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

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Let's check that panic! doesn't leak memory.

$ rustc panic.rs && valgrind ./panic

==4401== Memcheck, a memory error detector

==4401== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.

==4401== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info

==4401== Command: ./panic

==4401==

thread '<main>' panicked at 'division by zero', panic.rs:5

==4401==

==4401== HEAP SUMMARY:

==4401== in use at exit: 0 bytes in 0 blocks

==4401== total heap usage: 18 allocs, 18 frees, 1,648 bytes allocated

==4401==

==4401== All heap blocks were freed -- no leaks are possible

==4401==

==4401== For counts of detected and suppressed errors, rerun with: -v

==4401== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Where vectors store values by an integer index, HashMaps store values by key. HashMap keys can be booleans, integers, strings, or any other type that implements the Eq and Hash traits. More on this in the next section.

Like vectors, HashMaps are growable, but HashMaps can also shrink themselves when they have excess space. You can create a HashMap with a certain starting capacity using HashMap::with_capacity(uint), or use HashMap::new() to get a HashMap with a default initial capacity (recommended).

use std::collections::HashMap;

fn call(number: &str) -> &str {

match number {

"798-1364" => "We're sorry, the call cannot be completed as dialed.

Please hang up and try again.",

"645-7689" => "Hello, this is Mr. Awesome's Pizza. My name is Fred.

What can I get for you today?",

_ => "Hi! Who is this again?"

}

}

fn main() {

let mut contacts = HashMap::new();

contacts.insert("Daniel", "798-1364");

contacts.insert("Ashley", "645-7689");

contacts.insert("Katie", "435-8291");

contacts.insert("Robert", "956-1745");

// Takes a reference and returns Option<&V>

match contacts.get(&"Daniel") {

Some(&number) => println!("Calling Danieclass="underline" {}", call(number)),

_ => println!("Don't have Daniel's number."),

}

// `HashMap::insert()` returns `None`

// if the inserted value is new, `Some(value)` otherwise

contacts.insert("Daniel", "164-6743");

match contacts.get(&"Ashley") {

Some(&number) => println!("Calling Ashley: {}", call(number)),

_ => println!("Don't have Ashley's number."),

}

contacts.remove(&"Ashley");

// `HashMap::iter()` returns an iterator that yields

// (&'a key, &'a value) pairs in arbitrary order.

for (contact, &number) in contacts.iter() {