Guest User

Untitled

a guest
Feb 16th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  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. }
Add Comment
Please, Sign In to add comment