Skip to content

Instantly share code, notes, and snippets.

e-t-u

Block or report user

Report or block e-t-u

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View rust-error-cheatsheet.md

Rust Error Handling Cheatsheet - Result handling functions

Introduction to Rust error handling

Rust error handling is nice but obligatory. Which makes it sometimes plenty of code.

Functions return values of type Result that is "enumeration". In Rust enumeration means complex value that has alternatives and that alternative is shown with a tag.

Result is defined as Ok or Err. The definition is generic, and both alternatives have

@e-t-u
e-t-u / gist:0847039a6b45d90718f39a8cf4643c96
Last active Feb 14, 2020
Rust Error Handling Cheat Sheet, Result manipulation functions
View gist:0847039a6b45d90718f39a8cf4643c96
# Rust Error Handling Cheatsheet - Result handling functions
| What to do with | | Code |
| Ok t:T | Err e:E | r: Result<T, E> | Description
|-------------------------------|---------------------------------------------|-----------------------------------|----------
| ## mapping result to result | | |
| t -> Result::Ok(t2) | e -> e2 | r.map_or_else(|e| g(e), |t| f(t)) | Map both Ok and Err with function, function can not return Result (errror), note that Err mapping function is first (it is considered as a default like in the map_or)
| t -> t2 (Ok or Err) | Literal (default) | r.map_or(literal, |t| f(t)) | Map with literal but if error, use default literal, mapping function can be a
You can’t perform that action at this time.