Skip to content

Instantly share code, notes, and snippets.

@DougAnderson444
Created June 27, 2025 16:27
Show Gist options
  • Save DougAnderson444/04b7dcd6bffa197c0ffaf211d24c7c62 to your computer and use it in GitHub Desktop.
Save DougAnderson444/04b7dcd6bffa197c0ffaf211d24c7c62 to your computer and use it in GitHub Desktop.
Rust.map.unwrap_or_else

🚀 Simplifying your Rust Results! Technically Result is an enum, so you can match on it:

// Instead of this:
match some_function(&input) {
    Ok(value) => process_value(value),
    Err(err) => {
        log_error(err);
    }
}

But Result comes with great functional methods built in to streamline your code:

// Use this:
some_function(&input)
    .map(|value| process_value(value))
    .unwrap_or_else(|err| {
        log_error(err);
        handle_error();
    });

Cleaner, more concise, and still handles errors effectively! 💡 #RustLang #CodingTips #ErrorHandling

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment