Skip to content

Instantly share code, notes, and snippets.

@0ex-d
Created January 24, 2025 18:06
Show Gist options
  • Save 0ex-d/4c08ef214df80a7d2814156bc6a78164 to your computer and use it in GitHub Desktop.
Save 0ex-d/4c08ef214df80a7d2814156bc6a78164 to your computer and use it in GitHub Desktop.
// abstract from std
{
enum Result <T, E> {
Ok(()),
Err(())
}
enum Option <T> {
Some(()),
None
}
}
// 1. Option
// ✅ one exact expectation (exists or none)
HashMap::get() -> Option <&V>;
// ❌ uneccesary declaration of Error
HashMap::get() -> Result <&V, NotExistError>;
// 2. Result
enum Utf8Error {WeirdByte, UnpadedByte};
// ✅ one expectation but possible reason for not getting right expectation
std::str::from_utf8() -> Result<&str, Utf8Error>;
// ❌ just like above, but using Option is not "verbose" to the user
std::str::from_utf8() -> Option<&str>;
// 3. ? operator - postfix
match operand {
Ok(ok) => ok,
Err(bad) => Err(From::from(bad)), // Err() = ! = never
}
do_fallible_stuff()?;
Ok((do_fallible_stuff()?, do_fallible_stuff()?))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment