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

**URL:** <https://users.rust-lang.org/t/state-of-const-fn-for-producing-str-at-compile-time/28627>\
**Category:** uncategorized\
**Created:** [May 26, 2019, 6:09pm UTC](https://users.rust-lang.org/t/state-of-const-fn-for-producing-str-at-compile-time/28627 "2019-05-26T18:09:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![KillTheMule](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/killthemule/32/4748_2.png) [@KillTheMule](https://users.rust-lang.org/u/KillTheMule)\
**Post date:** [May 26, 2019, 6:09pm UTC](https://users.rust-lang.org/t/state-of-const-fn-for-producing-str-at-compile-time/28627/1 "2019-05-26T18:09:57Z")

</div>

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

```rust
---------------X

```

(padded to 16), so that in the end this produces  
`---IDNOD---------------X`

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 🙂

---

<div class="post-metadata">

**Author:** ![dtolnay](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/dtolnay/32/2115_2.png) [@dtolnay](https://users.rust-lang.org/u/dtolnay)\
**Post date:** [May 26, 2019, 9:26pm UTC](https://users.rust-lang.org/t/state-of-const-fn-for-producing-str-at-compile-time/28627/2 "2019-05-26T21:26:59Z")

</div>

I suspect this would be better suited to a procedural macro than const fn. String concatenation in const fn is currently [complicated](https://github.com/Vurich/const-concat). 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"`.

---

<div class="post-metadata">

**Author:** ![KillTheMule](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/killthemule/32/4748_2.png) [@KillTheMule](https://users.rust-lang.org/u/KillTheMule)\
**Post date:** [May 27, 2019, 6:36am UTC](https://users.rust-lang.org/t/state-of-const-fn-for-producing-str-at-compile-time/28627/3 "2019-05-27T06:36:49Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [August 25, 2019, 6:36am UTC](https://users.rust-lang.org/t/state-of-const-fn-for-producing-str-at-compile-time/28627/4 "2019-08-25T06:36:54Z")

</div>

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