Skip to main content

mkctx

Macro mkctx 

Source
macro_rules! mkctx {
    ($fmt:literal $($args:tt)*) => { ... };
}
Expand description

Creates a lazily-evaluated context from a format string.

If the format string contains only a literal, it will be converted to a typed literal. This eliminates all allocations when it’s the only component of the error, e.g. building a stateless error from an Option.

§Examples

// A plain literal, no allocation.
foo().with_context(mkctx!("file not found"))?;
// A runtime value, one allocation for the error.
foo().with_context(stream_id)?;
// With format args, the format string adds a second allocation when materializing the error.
foo().with_context(mkctx!("failed to read from stream {stream_id}"))?;