Crates.io | expect_with |
lib.rs | expect_with |
version | 1.0.0 |
source | src |
created_at | 2023-03-06 20:36:23.458205 |
updated_at | 2023-03-06 20:36:23.458205 |
description | Expect with formatting |
homepage | https://github.com/Forestryks/expect_with |
repository | https://github.com/Forestryks/expect_with |
max_upload_size | |
id | 802960 |
size | 3,988 |
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.
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()));
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.