Is there any way to pre-process a string at compile time?

Is there anyway to pre-process a string compile time. For example, It would be really useful to replace a certain character compile time.

// this
println!("Some test string".replace(' ', '-'));

//would compile as this
println!("Some-test-string");

// giving this 
// output
//
// Some-test-string
//

Is this possible in any way?

1 Like

The println! macro requires a string literal (not even a const will work), so the replacement would need to be done by another macro.

The standard library doesn’t have a macro for replacing characters in string literals, and I haven’t found any external libraries that do it either, but you could write your own procedural macro.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.