Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.40 KB | None | 0 0
  1. macro_rules! guard {
  2. ($o:expr) => {
  3. if let Some(x) = $o { x } else { return; }
  4. };
  5. ($o:expr, $body:block) => {
  6. if let Some(x) = $o { x } else { $body; return; }
  7. };
  8. }
  9.  
  10. fn main() {
  11. let x = Some("hello");
  12. let y = guard!(x);
  13. println!("{}", y);
  14.  
  15. let x = None;
  16. let y = guard!(x, {
  17. println!("Nothing to see here!");
  18. });
  19. println!("{:?}", y);
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement