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?
You can create a macro:
Note that I wouldn't recommend discarding the errors. Rust's error handling is pretty ergonomic, so I would return the error, even if it is only to log it:
If both functions return
Result<doesn't matter, same T>
you can just put a?
at the end of line of call.You can use same pattern for
Option<T>
too.