๐Ÿ‘ฅ Rust Monads

(not an official identifier, used for simplification)

Types

Shared Functions

Note: monad / Monad stands for Option or Result

Unwrap

  • monad.unwrap_or(fallback) โ†’ returns success value, fallback if it doesnโ€™t exist
  • monad.unwrap_or_else(fallback_fn) โ†’ returns success value, calls fallback fn / closure if it doesnโ€™t exist
  • monad.unwrap() โ†’ returns success value, panic if it doesnโ€™t exist
  • monad.expect(message) โ†’ like .unwrap(), also prints message

Map

  • monad.map(self, f: F) โ†’ Maps a Result<T, E> to Result<U, E> by applying a function/closure op to it, fail value untouched
  • monad.map_err(self, op: F) โ†’ like map(), but for E
  • monad.map_or(self, default,f: F) โ†’ Maps a Result<T, E> to Result<U, E> by applying a function/closure f to it, or returns default if E exists
  • monad.map_or_else(self, default, op: F) โ†’ lazy call of map_or, use if passing function call as default

References

  • monad.as_ref() โ†’ Converts &Monad<T> to Monad<&T>
  • monad.as_mut() โ†’ Converts &mut Monad<T> to Monad<&mut T>