expect_with

Crates.ioexpect_with
lib.rsexpect_with
version1.0.0
sourcesrc
created_at2023-03-06 20:36:23.458205
updated_at2023-03-06 20:36:23.458205
descriptionExpect with formatting
homepagehttps://github.com/Forestryks/expect_with
repositoryhttps://github.com/Forestryks/expect_with
max_upload_size
id802960
size3,988
Andrei Odintsov (Forestryks)

documentation

README

expect_with

Crates.io Documentation

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.

Commit count: 2

cargo fmt