Search code examples
rustconstants

Is there a way to concat strings/bytes at compile time?


Consider this example:

const TARGET_DIRECTORY: &'static str = "files";
const INDEX_FILE_PATH: &'static str = TARGET_DIRECTORY + "/index.txt"; // Should resolve to "files/index.txt"

This falters since the string on the left (TARGET_DIRECTORY) needs to be a String in order to use the + operator. One might try invoking TARGET_DIRECTORY.to_owned(), but that can't be executed at compilation.


Solution

  • I found the constcat crate right after posting. There's other ones but this one is the simplest and smallest one. I'll mark this as an answer in 2 days.