Macro interpolator::context

source ·
macro_rules! context {
    ($($tts:tt)*) => { ... };
}
Expand description

Creates a context for use with this crate’s format() and write() functions.

It takes a comma seperated list of idents, followed by optional format specifiers and an optional value.

  • a => key = "a", value = a, traits = [Display]
  • a:? => key = "a", value = a, traits = [Debug]
  • a: :? => key = "a", value = a, traits = [Debug, Display]
  • a = 5 => key = "a", value = 5, traits = [Display]
  • a:?:x = 5 => key = "a", value = 5, traits = [Debug, LowerHex]
use interpolator::context;

let a = "hello";
let context = context! {
    a,
    a:?, // This does not override the display implementation, only debug
    a: :?, // This overrides both display and debug
    a = 5, // Only overrides display
    a:?:x = 5, // Sets debug and lower hex implementation
};

Due to borrow rules you might need to create let bindings for the values, i.e.

let test = String::from("test");
let context = context!(test);

instead of

let context = context!(test = String::from("test"));