Skip to content

Instantly share code, notes, and snippets.

@wycats
Last active October 21, 2018 12:04
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save wycats/d52fdf26f96f1636ea9525f7d3fca1b4 to your computer and use it in GitHub Desktop.
Save wycats/d52fdf26f96f1636ea9525f7d3fca1b4 to your computer and use it in GitHub Desktop.
How I think about error handling in Rust
  • Option: "None is a totally valid result, usually found in data structures"
    • unwrap(): YOLO use in prototyping
    • expect(...): to indicate the reason you believe None is impossible (or a contract violation for the method)
  • Result: 😱 "Errors are unexpected but not bugs; the decision for how to handle them is up to the caller"
    • unwrap(): YOLO "I'm an app and don't know how to handle this error -- I'm fine if the whole process aborts"
    • try! / ?: "Leave the decision about how to handle this error to the caller; they have more information"
    • match / catch: "I'm going to handle the error right here right now"
  • panic!: ☠ "The error is a bug and can't be recovered from. Game over man. Rust is allowed to abort the process if it wants"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment