fast_concat

Macro fast_concat 

Source
fast_concat!() { /* proc-macro */ }
Expand description

Concatenates string expressions.

If you only pass in literals or constants, you will get a const &'static str back. Otherwise, this macro will create a buffer with the optimal capacity and push every string to it.

§Syntax

Any amount of expressions that evaluate to a &str separated by commas. An expression can be prefixed with const to indicate that it is constant.

§Examples

const CONST: &str = "const ";
let var = "var ";
let mut buf = String::new();

// use const keyword to indicate that it is constant
let expansion = fast_concat!("lit0 ", const CONST, var, "lit1 ", "lit2 ", 9, {
    for i in 0..10 {
        buf.push_str(&i.to_string());
    }

    &buf
});

buf.clear();

// what the value is
assert_eq!(expansion, "lit0 const var lit1 lit2 90123456789");

// what code gets generated
assert_eq!(expansion, {
    extern crate alloc;
    // constcat generates these
    const _0: &'static str = "lit0 const ";
    let _1: &str = var;
    const _2: &'static str = "lit1 lit2 9";
    let _3: &str = {
        for i in 0..10 {
            buf.push_str(&i.to_string());
        }

        &buf
    };

    let mut buf = alloc::string::String::with_capacity(0 + _0.len() + _1.len() + _2.len() + _3.len());
    buf.push_str(_0);
    buf.push_str(_1);
    buf.push_str(_2);
    buf.push_str(_3);
    buf
});

const ASSETS_DIR: &str = "./assets";
const ICON: &str = fast_concat!(const ASSETS_DIR, '/', "icon.png");

assert_eq!(ICON, "./assets/icon.png");
assert_eq!(ICON, {
    const OUTPUT: &'static str = ::constcat::concat!(ASSETS_DIR, '/', "icon.png" , );
    OUTPUT
});