HiveBrain v1.2.0
Get Started
← Back to all entries
snippetrustMajor

How do you unwrap a Result on Ok or return from the function on Err?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
unwrapyouhowreturnfromfunctionerrtheresult

Problem

I have a function that calls another function which returns a Result. I need to check if the Result is Ok or Err and if it is an Err, I need to return early from my function. This is what I'm doing now:

match callable(&mut param) {
    Ok(_v) => (),
    Err(_e) => return,
};


Is there a more idiomatic Rust way to do this?

Solution

Rust 1.65.0 has stabilized let-else statements, which enables you to write:
let Ok(_v) = callable(&mut param) else { return };

Context

Stack Overflow Q#51344951, score: 61

Revisions (0)

No revisions yet.