SHARE
TWEET

Untitled

a guest Feb 16th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Option type
  2.  
  3. fn safe_divide(dividend: i64, divisor: i64) -> Option<i64> {
  4.     if 0 == divisor {
  5.         None
  6.     } else {
  7.         Some(dividend / divisor)
  8.     }
  9. }
  10.  
  11. fn main() {
  12.    
  13.     let x = 10;
  14.     let y = 5;
  15.    
  16.     match safe_divide(x, y) {
  17.         Some(quotient) => println!("{} / {} = {}", x, y, quotient),
  18.         None           => panic!("the disco!"),
  19.     }
  20.    
  21.     let y = 0;
  22.    
  23.     match safe_divide(x, y) {
  24.         Some(quotient) => println!("{} / {} = {}", x, y, quotient),
  25.         _otherwise     => panic!("the disco!"),
  26.     }
  27.    
  28. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top