1 stable release
1.0.0 | Mar 6, 2023 |
---|
#17 in #errors
4KB
expect_with
Adds expect_with()
for Option<T>
and Result<T, E>
where E
is Debug
(all std types that has expect
method).
This method functions exactly the same as except()
but evaluates error message only when actual error occurred.
Example
fn some_heavy_function() -> String {
return String::from("42");
}
let result: Result<(), String> = Err(String::from("some error"));
result.expect_with(|| format!("error {}", some_heavy_function()));
Motivation
Using expect
has one major drawback. It will calculate it's argument every time. Even if no error occurred. This can be really slow if expect
is called frequently and evaluating error message envolves some computing (even simple format
can be awfully slow). expect_with
removes this overhead by accepting lambda, which will be executed to get error message only when needed.