Skip to content

Instantly share code, notes, and snippets.

@bvssvni
Created March 21, 2014 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bvssvni/9684061 to your computer and use it in GitHub Desktop.
Save bvssvni/9684061 to your computer and use it in GitHub Desktop.
let x = Some(1);
let y = Some(2);
let z = monad!(
None {
_ => x,
Some(x) => y,
Some(y) => Some(x+y)
}
);
assert_eq!(z, Some(3));
@bvssvni
Copy link
Author

bvssvni commented Mar 21, 2014

This works because in Rust you can shadow variable names. Each arm matches against the result of the previous arm. Because the pattern matching allows binding of variables to a name, you can 'unwrap' x and y before adding them together.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment