# As\_str for integers

**URL:** <https://users.rust-lang.org/t/as-str-for-integers/142364>\
**Category:** help\
**Created:** [September 9, 2026, 10:52pm UTC](https://users.rust-lang.org/t/as-str-for-integers/142364 "2026-09-09T22:52:51Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![bachchain](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@bachchain](https://users.rust-lang.org/u/bachchain)\
**Post date:** [September 9, 2026, 10:52pm UTC](https://users.rust-lang.org/t/as-str-for-integers/142364/1 "2026-09-09T22:52:51Z")

</div>

I'm looking for ways to have str representations of ranges of integers without allocating. My current best attempt is to manually match on each value:

```rust
match i
{
    0 => "0",
    1 => "1",
    2 => "2",
    ...
    n => "n"
}

```

which works, but has the obvious problem of not scaling. I've tried to automate this with macros, but i couldn't get it to work with declarative macros and procedural macros seemed too involved for me.

---

<div class="post-metadata">

**Author:** ![simonbuchan](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/simonbuchan/32/19939_2.png) [@simonbuchan](https://users.rust-lang.org/u/simonbuchan)\
**Post date:** [September 9, 2026, 11:03pm UTC](https://users.rust-lang.org/t/as-str-for-integers/142364/2 "2026-09-09T23:03:17Z")

</div>

If you think about this for a second, doing this for u32 requires you to have ~4 billion statically allocated strings up to 10 bytes in length. Arguably you could overlap and share prefixes, but that is only likely to save you around 10%, so you're looking down the barrel of 40 GB here.

Obviously that's not tenable.

Instead if by "not allocating" you simply mean "not requiring a heap allocation" you could instead write into a buffer/small string type that can hold up to 10 bytes and be dereferenced to &str, but even that's pretty pointless since what you nearly certainly want to do with this string is print or otherwise format it, and that's directly supported by the integer itself using Display.

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cuviper/32/3156_2.png) [@cuviper](https://users.rust-lang.org/u/cuviper)\
**Post date:** [September 9, 2026, 11:16pm UTC](https://users.rust-lang.org/t/as-str-for-integers/142364/3 "2026-09-09T23:16:10Z")

</div>

> [@simonbuchan](#):
>
> Instead if by "not allocating" you simply mean "not requiring a heap allocation" you could instead write into a buffer/small string type that can hold up to 10 bytes and be dereferenced to &str,

You don't have to write that yourself:

> **[NumBuffer in core::fmt - Rust](https://doc.rust-lang.org/nightly/core/fmt/struct.NumBuffer.html)**
>
> Memory for formatting numbers using \`T::format\_into()\`.

---

<div class="post-metadata">

**Author:** ![bachchain](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@bachchain](https://users.rust-lang.org/u/bachchain)\
**Post date:** [September 9, 2026, 11:27pm UTC](https://users.rust-lang.org/t/as-str-for-integers/142364/4 "2026-09-09T23:27:03Z")

</div>

I don't need the entire value space. Currently, I only need from 0 to around 4000, which, if my math is correct, should only require about 15k.

---

<div class="post-metadata">

**Author:** ![tczajka](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/tczajka/32/24743_2.png) [@tczajka](https://users.rust-lang.org/u/tczajka)\
**Post date:** [September 9, 2026, 11:40pm UTC](https://users.rust-lang.org/t/as-str-for-integers/142364/5 "2026-09-09T23:40:05Z")

</div>

Don't use macros, write a `const` function that computes the array and store it in a `static` variable.

---

<div class="post-metadata">

**Author:** ![jdahlstrom](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jdahlstrom/32/38017_2.png) [@jdahlstrom](https://users.rust-lang.org/u/jdahlstrom)\
**Post date:** [September 9, 2026, 11:40pm UTC](https://users.rust-lang.org/t/as-str-for-integers/142364/6 "2026-09-09T23:40:09Z")

</div>

Nonetheless, `format_into()` gives you the whole range without using any additional memory. Its only problem is it's not `const` yet. Is it really important for you for some reason to have the numerals hard-coded in the executable as static strings?

---

<div class="post-metadata">

**Author:** ![nerditation](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@nerditation](https://users.rust-lang.org/u/nerditation)\
**Post date:** [September 9, 2026, 11:40pm UTC](https://users.rust-lang.org/t/as-str-for-integers/142364/7 "2026-09-09T23:40:16Z")

</div>

> [@bachchain](#):
>
> Currently, I only need from 0 to around 4000, which, if my math is correct, should only require about 15k.

if the static storage is not a problem for your use case, you can just generate the lookup table in a build script.

---

<div class="post-metadata">

**Author:** ![bachchain](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@bachchain](https://users.rust-lang.org/u/bachchain)\
**Post date:** [September 9, 2026, 11:46pm UTC](https://users.rust-lang.org/t/as-str-for-integers/142364/8 "2026-09-09T23:46:09Z")

</div>

I had preferred statics because I'm also doing a fair amount of cross-thread communication and I didn't want the additional lifetime considerations or dealing with Arcs

---

<div class="post-metadata">

**Author:** ![nerditation](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@nerditation](https://users.rust-lang.org/u/nerditation)\
**Post date:** [September 10, 2026, 12:17am UTC](https://users.rust-lang.org/t/as-str-for-integers/142364/9 "2026-09-10T00:17:42Z")

</div>

> [@bachchain](#):
>
> I'm also doing a fair amount of cross-thread communication

why you send the string representations cross threads? why not just send the integer values and format the number in each thread?

---

<div class="post-metadata">

**Author:** ![throwable-one](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/throwable-one/32/31948_2.png) [@throwable-one](https://users.rust-lang.org/u/throwable-one)\
**Post date:** [September 10, 2026, 1:18am UTC](https://users.rust-lang.org/t/as-str-for-integers/142364/10 "2026-09-10T01:18:35Z")

</div>

I wonder if you really need all these strings, or could simply have

```rust
const DIGITS: &str = "0123456789";

```

and cherry-pick substrings from it, but it depends on your needs.

I mean

```rust
use std::io::{Write, stdout};

const DIGITS: &str = "0123456789";
fn main() {
    let mut num = 1234;
    let mut divisor = 1u32;
    while num / divisor >= 10 {
        divisor *= 10;
    }
    while divisor > 0 {
        let i = (num / divisor) as usize;
        let c = &DIGITS[i..i + 1];
        stdout().lock().write(c.as_bytes()).unwrap();
        num %= divisor;
        divisor /= 10;
    }
}
``
```

---

<div class="post-metadata">

**Author:** ![simonbuchan](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/simonbuchan/32/19939_2.png) [@simonbuchan](https://users.rust-lang.org/u/simonbuchan)\
**Post date:** [September 10, 2026, 1:45am UTC](https://users.rust-lang.org/t/as-str-for-integers/142364/11 "2026-09-10T01:45:08Z")

</div>

You've just reinvented Display (though locking per character, which is odd)

---

<div class="post-metadata">

**Author:** ![throwable-one](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/throwable-one/32/31948_2.png) [@throwable-one](https://users.rust-lang.org/u/throwable-one)\
**Post date:** [September 10, 2026, 1:59am UTC](https://users.rust-lang.org/t/as-str-for-integers/142364/12 "2026-09-10T01:59:27Z")

</div>

I used stdout just to illustrate the idea:) My point was to store a small static string of all possible digits, and cherry-pick them.

---

<div class="post-metadata">

**Author:** ![tczajka](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/tczajka/32/24743_2.png) [@tczajka](https://users.rust-lang.org/u/tczajka)\
**Post date:** [September 10, 2026, 2:01am UTC](https://users.rust-lang.org/t/as-str-for-integers/142364/13 "2026-09-10T02:01:49Z")

</div>

> [@throwable-one](#):
>
> My point was to store a small static string of all possible digits

You don't need to store a precomputed string of all possible digits to be able to trivially turn a number `0..=9` into a digit. `b'0' + x` (or `b'0' | x`) will do it.

---

<div class="post-metadata">

**Author:** ![throwable-one](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/throwable-one/32/31948_2.png) [@throwable-one](https://users.rust-lang.org/u/throwable-one)\
**Post date:** [September 10, 2026, 2:05am UTC](https://users.rust-lang.org/t/as-str-for-integers/142364/14 "2026-09-10T02:05:43Z")

</div>

Even better, thsnks!. So, the question is why does topic starter need 4K strings?

PS: or it also could be..

```rust
let start = '0' as u8;
let i = (num / divisor) as u8;
let c = (start + i) as char;

```

---

<div class="post-metadata">

**Author:** ![chrefr](https://avatars.discourse-cdn.com/v4/letter/c/e480ec/32.png) [@chrefr](https://users.rust-lang.org/u/chrefr)\
**Post date:** [September 10, 2026, 4:43am UTC](https://users.rust-lang.org/t/as-str-for-integers/142364/15 "2026-09-10T04:43:33Z")

</div>

Because they do not want an owned buffer but a `&'static str`.

If you want a buffer, you can use `Display` or recently [`format_into()`](https://doc.rust-lang.org/stable/std/primitive.u32.html#method.format_into) like said above.

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/quinedot/32/17693_2.png) [@quinedot](https://users.rust-lang.org/u/quinedot)\
**Post date:** [September 10, 2026, 6:39am UTC](https://users.rust-lang.org/t/as-str-for-integers/142364/16 "2026-09-10T06:39:34Z")

</div>

(Not a serious suggestion.)

> [@simonbuchan](#):
>
> Arguably you could overlap and share prefixes, but that is only likely to save you around 10%, so you're looking down the barrel of 40 GB here.

The repeating pattern of the decimal representation of `1/10_000_000_019` contains every zero-padded ten-digit number at least once, so you "only" need ~10G (at the cost of non-constant lookup).

> [@bachchain](#):
>
> Currently, I only need from 0 to around 4000, which, if my math is correct, should only require about 15k.

Or for (all) four-digit numbers, [analogously store `1/10_007`'s pattern in ~10K.](https://play.rust-lang.org/?version=stable&mode=release&edition=2024&gist=ae5af713d913b6b8baa6acd96fe48ea3)\[1\]

* * *

1. Edit: made an `unsafe` free version

---

<div class="post-metadata">

**Author:** ![simonbuchan](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/simonbuchan/32/19939_2.png) [@simonbuchan](https://users.rust-lang.org/u/simonbuchan)\
**Post date:** [September 10, 2026, 7:35am UTC](https://users.rust-lang.org/t/as-str-for-integers/142364/17 "2026-09-10T07:35:32Z")

</div>

> [@quinedot](#):
>
> The repeating pattern of the decimal representation of `1/10_000_000_019` contains every zero-padded ten-digit number at least once

Every so often I am told about some maths fact that I can only assume someone went to prison for discovering

---

<div class="post-metadata">

**Author:** ![m-r](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/m-r/32/37491_2.png) [@m-r](https://users.rust-lang.org/u/m-r)\
**Post date:** [September 10, 2026, 9:06am UTC](https://users.rust-lang.org/t/as-str-for-integers/142364/18 "2026-09-10T09:06:25Z")

</div>

on my guts, the denominator is `10^n + prime` so I don't think _that much_ brute force was involved

---

<div class="post-metadata">

**Author:** ![simonbuchan](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/simonbuchan/32/19939_2.png) [@simonbuchan](https://users.rust-lang.org/u/simonbuchan)\
**Post date:** [September 10, 2026, 9:14am UTC](https://users.rust-lang.org/t/as-str-for-integers/142364/19 "2026-09-10T09:14:23Z")

</div>

Yeah there's going to be some pigeon hole principle or something. It's just that it's one of the specific types of maths that my brain fits into a "wanting to know this is a sign of a diseased mind" bucket for some reason. It's like an Ignobel prize but for the Fields medal.

---

<div class="post-metadata">

**Author:** ![jendrikw](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@jendrikw](https://users.rust-lang.org/u/jendrikw)\
**Post date:** [September 10, 2026, 10:49am UTC](https://users.rust-lang.org/t/as-str-for-integers/142364/20 "2026-09-10T10:49:49Z")

</div>

> [@quinedot](#):
>
> `1/10_000_000_019` contains every zero-padded ten-digit number at least once, so you "only" need ~10G (at the cost of non-constant lookup).

Isn't this the problem of [superpermutations](https://en.wikipedia.org/wiki/Superpermutation), which has a lower bound of n! + (n-1)! + (n-2)! + n-3 as proven by the famous 4chan post ([/sci/ - Science & Math - #3751105](https://warosu.org/sci/thread/S3751105#p3751197))? In the case of n=10, the lower bound should be around [4 million](https://www.wolframalpha.com/input?i=10%21+%2B+%2810-1%29%21+%2B+%2810-2%29%21+%2B+10+-+3). I don't know how to find the index into this array tho.

[Next page](https://users.rust-lang.org/t/as-str-for-integers/142364.md?page=2)
