Function xpct::why_lazy

source ·
pub fn why_lazy<'a, In, PosOut, NegOut>(
    matcher: Matcher<'a, In, PosOut, NegOut>,
    reason: impl Fn() -> Cow<'a, str> + 'a
) -> Matcher<'_, In, PosOut, NegOut>where
    In: 'a,
    PosOut: 'a,
    NegOut: 'a,
Expand description

Attaches a lazily evaluated context string to a matcher that will appear in the failure output.

This serves the same purpose as why, except it can be used when the context value would be expensive to compute.

Example

use std::borrow::Cow;
use xpct::{expect, why_lazy, be_ge};

// Imagine this is expensive to compute.
fn expensive_context() -> Cow<'static, str> {
    Cow::Borrowed("indices must be positive")
}

let index = 2_i32;

expect!(index).to(why_lazy(
    be_ge(0),
    expensive_context
));