I'm trying to concatenate static strings and string literals to build another static string. The following is the best I could come up with, but it doesn't work:
const DESCRIPTION: &'static str = "my program";
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
const VERSION_STRING: &'static str = concat!(DESCRIPTION, " v", VERSION);
Is there any way to do that in Rust or do I have to write the same literal over and over again?
The compiler error is
A literal is anything you type directly like
"hello"
or5
. The moment you start working with constants, you are not using literals anymore, but identifiers. So right now the best you can do isSince the
env!
macro expands to a literal, you can use it insideconcat!
.Since I was essentially trying to emulate C macros, I tried to solve the problem with Rust macros and succeeded:
It feels a bit ugly to use macros instead of constants, but it works as expected.