lformat

Macro lformat 

Source
macro_rules! lformat {
    ($c:literal, $($arg:tt)*) => { ... };
    ($s:ty, $($arg:tt)*) => { ... };
}
Available on crate feature arraystring only.
Expand description

Formats an ArrayString possibly truncating the result.

This macro, similar to the standard std::format!, formats a string but with ArrayString as the resulting type. This allows formatting a string on stack, without memory allocation.

Note that, as ArrayString is a fixed-capacity non-growable string, the result may be truncated (on character boundary) to fit the given capacity.

This macro is a convenience wrapper of the format_lossy function.

ยงExamples

Format an ArrayString specifying the capacity only. The resulting type uses Usize as length type and Uninitialized as spare memory policy.

let s = lformat!(16, "Hello, world!");
assert_eq!(s, "Hello, world!");
assert_eq!(s.capacity(), 16);

Format an ArrayString specifying the array-string type. This allows customization of length type and spare memory policy.

type A = ArrayString<16, U8, Pattern<0xCD>>;
let s = lformat!(A, "Hello, world!");
assert_eq!(s, "Hello, world!");

Note that the result may be truncated if ArrayString capacity is not enough to accommodate the whole formatted string. In some use case this may yield wrong results. Thus use only where lossy formatting is appropriate, or when the capacity is ensured to be enough.

let s = lformat!(5, "a=2500");
assert_eq!(s, "a=250");  // <-- !! the result may be wrong in some use cases