Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- macro_rules! maybe {
- ($x:expr) => {
- match $x {
- None => return None,
- Some(x) => x,
- }
- }
- }
- fn maybe_divide(x: Option<u32>, y: Option<u32>) -> Option<u32> {
- let result = maybe!(x) / maybe!(y);
- Some(result)
- }
- fn main() {
- let a = maybe_divide(Some(10), Some(2));
- let b = maybe_divide(None, Some(2));
- let c = maybe_divide(Some(10), None);
- let d = maybe_divide(None, None);
- println!("{:?} {:?} {:?} {:?}", a, b, c, d);
- }
- // Write a macro, `guard`, which takes a condition and returns `None` if the
- // condition is not true.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement