Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. macro_rules! maybe {
  2. ($x:expr) => {
  3. match $x {
  4. None => return None,
  5. Some(x) => x,
  6. }
  7. }
  8. }
  9.  
  10. fn maybe_divide(x: Option<u32>, y: Option<u32>) -> Option<u32> {
  11. let result = maybe!(x) / maybe!(y);
  12. Some(result)
  13. }
  14.  
  15. fn main() {
  16. let a = maybe_divide(Some(10), Some(2));
  17. let b = maybe_divide(None, Some(2));
  18. let c = maybe_divide(Some(10), None);
  19. let d = maybe_divide(None, None);
  20.  
  21. println!("{:?} {:?} {:?} {:?}", a, b, c, d);
  22. }
  23.  
  24. // Write a macro, `guard`, which takes a condition and returns `None` if the
  25. // condition is not true.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement