#format! with static str

6 messages ยท Page 1 of 1 (latest)

hearty jackal
#

Hi peeps! Need help and don't even know what i need to google ๐Ÿ˜„ i'm trying to use static str as a template for format!(static str, ...) as i would in other languages. this is obviously making rustc mad ๐Ÿ˜„ what is an idiomatic way to do this in rust? example that is giving me errors:

static LEX_ERROR_UNEXPECTED_CHAR: &str = "Unexpected character {} at {}:{}!";
...
                    return Err(format!(LEX_ERROR_UNEXPECTED_CHAR, c, self.ln, self.col));

Compile error is:

  --> src/main.rs:91:40
   |
91 |                     return Err(format!(LEX_ERROR_UNEXPECTED_CHAR, c, self.ln, self.col));
   |                                        ^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: you might be missing a string literal to format with
   |
91 |                     return Err(format!("{} {} {} {}", LEX_ERROR_UNEXPECTED_CHAR, c, self.ln, self.col));
   |                                        ++++++++++++++

prisma flint
#

the way would be to not use a static variable

#

and just use the string literal

#

if you need to do it in more than one place, you should write a function or a macro

hearty jackal
#

aha ok, thx! get it

wraith siren
#

?eval

macro_rules! thrice {
    ($arg:tt) => {
        concat!(
            "{", stringify!($arg), "}",
            "{", stringify!($arg), "}",
            "{", stringify!($arg), "}",
        )
    }
}

format!(thrice!(0), "hello")