[][src]Macro const_format::strwriter_as_str

macro_rules! strwriter_as_str {
    ($expr:expr) => { ... };
}

Converts a &'static StrWriter to a &'static str, in a const/static initializer.

This is usable in const or static initializers, but not inside of const fns.

Runtime

If the "constant_time_as_str" feature is disabled, thich takes time proportional to $expr.capacity() - $expr.len().

If the "constant_time_as_str" feature is enabled, it takes constant time to run, but uses a few additional nightly features.

Example

#![feature(const_mut_refs)]

use const_format::StrWriter;
use const_format::{strwriter_as_str, unwrap, writec};


const CAP: usize = 128;

// Can't use mutable references in `const`s yet, the `const fn` is a workaround
const fn formatted() -> StrWriter<[u8; CAP]> {
    let mut writer =  StrWriter::new([0; CAP]);

    // Writing the array with debug formatting, and the integers with hexadecimal formatting.
    unwrap!(writec!(writer, "{:x}", [3u32, 5, 8, 13, 21, 34]));

    writer
}

const STR: &str = strwriter_as_str!(&formatted());

fn main() {
    assert_eq!(STR, "[3, 5, 8, D, 15, 22]");
}