// The return value of the function is an option letresult = divide(2.0, 3.0);
// Pattern match to retrieve the value match result { // The division was valid Some(x) => println!("Resukt: {}", x), // The division was invalid None => println!("Cannot divide by 0"), }
// A list of data to search through. letall_the_big_things = [ Kingdom::Plant(250, "redwood"), Kingdom::Plant(230, "noble fir"), Kingdom::Plant(229, "sugar pine"), Kingdom::Animal(25, "blue whale"), Kingdom::Animal(19, "fin whale"), Kingdom::Animal(25, "north pacific right whale"), ];
// We're going to search for the name of the biggest animal, // but to start with we've just got `None`. letmut name_of_biggest_animal = None; letmut size_of_biggest_animal = 0; forbig_thingin &all_the_big_things { match *big_thing { Kingdom::Animal(size, name) if size > size_of_biggest_animal => { // Now we've found the name of some big animal size_of_biggest_animal = size; name_of_biggest_animal = Some(name); } Kingdom::Animal(..) | Kingdom::Plant(..) => () } }
match name_of_biggest_animal { Some(name) => println!("the biggest animal is {}", name), None => println!("there are no animals:("), }