To expand a bit, you can write your own macros that only accept literals (playground):
macro_rules! takes_literal {
($foo:literal) => { dbg!($foo); }
}
fn main() {
takes_literal!("foo"); // [src/main.rs:7:5] "foo" = "foo"
let foo = "foo";
takes_literal!(foo); // does not compile
const FOO: &str = "foo";
takes_literal!(FOO); // does not compile either
}
although it's not possible to implement a print-like macro from scratch yourself, as it needs compiler support to parse and validate the format string.
although it's not possible to implement a print-like macro from scratch yourself, as it needs compiler support to parse and validate the format string.
Sorry to nitpick, but the format_args! macro is available if you need to format "hello {a} {:2}" style strings. println! expands to include format_args!, though format_args! itself is a compiler builtin.
And if you need to parse a more complex syntax, you can use a proc macro which can perform completely arbitrary transformations to your Rust code.