Created
January 24, 2025 18:06
-
-
Save 0ex-d/4c08ef214df80a7d2814156bc6a78164 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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