State of `const fn` for producing `&str` at compile time

I have a hard time understanding the current state of const fn, so maybe someone can point of if 1) I can do what I want right now with it, and 2) if it will be possible at some somewhat-known point in the future.

I have some static structs, that contain sequences of (u8, Enum), where Enum is a flat C-like enum with a Enum -> &'static str implementation. For each entry of the sequence, I need to take the u8 and make a computation involving the &'static str to pad the said &'static str with dashes. After that, I need to concatenate all those things into a larger &'static str.

As a example, the sequence might be (8, IDNOD), (16, X), and the Enum -> &'static str function is just the trivial thing. I’ll then need to produce ---IDNOD (padded to 8) and


(padded to 16), so that in the end this produces

So, theoretically, all this info is present at compile time. The question is, can I make this a compile time computation? If so, how can I ensure that it’s really done? I might be able to just put const before all functions, but does that guarantee it’s not done at runtime?

Thanks for any pointers :slight_smile:

I suspect this would be better suited to a procedural macro than const fn. String concatenation in const fn is currently complicated. Instead you should be able to write a procedural macro that you invoke wherever your sequence of (u8, Enum) is declared and it could emit the right static KTM: &str = "---IDNOD---------------X".


Hey yeah, thanks for the suggestion, a proc macro sounds good! Do you know an example proc macro that “does something similar”? Just in case you have it ready, I’ll need to learn the first things about proc macros anyways :slight_smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.