Format numbers in constant expressions

Is it possible to format numbers (integer, in practice) in constant expressions? Like the example below:

struct MyNumber(i32);
impl MyNumber {
    const fn format_self(&self) -> &'static str {

I've tried the "const_format" and "const_str" crate, but both of them use const value in their internal implementation. Is there any other workaround?

The problem is that a const fn is not always constant, it is supposed to be callable at runtime too. And you can't get a &'static str representing a runtime value (unless you e.g. leak memory, but that's generally unwanted).

I haven't found one yet (though string concatenation isn't exactly the same type of operation as number formatting, so maybe you'll have more luck than me).

What if my function only needs to be called during compile time?

Maybe a macro system could do it, but I don't want to try to implement complex operations within macro. :frowning:


I'm trying to implement this with macros. Never wanted postfix macro to be stable so much.

Does this suit your needs?

My last solution was to wrap this in a macro, since only macros can be used to ensure const-only use. Anyway, thanks for the advice!