# &mut \[u8\] and core::fmt::Write

**URL:** <https://internals.rust-lang.org/t/mut-u8-and-core-write/23369>\
**Category:** libs\
**Created:** [August 12, 2025, 9:21am UTC](https://internals.rust-lang.org/t/mut-u8-and-core-write/23369 "2025-08-12T09:21:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Strackeror](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/strackeror/32/13499_2.png) [@Strackeror](https://internals.rust-lang.org/u/Strackeror)\
**Post date:** [August 12, 2025, 9:21am UTC](https://internals.rust-lang.org/t/mut-u8-and-core-write/23369/1 "2025-08-12T09:21:03Z")

</div>

So, in `std`, there's this quite elegant pattern that's available to format into a byte buffer:

```rust
use std::io::Write;
fn toast() {
    let mut buf = [0u8, 10];
    let mut slice = &mut buf[..];
    let integer = 0;

    write!(slice, "{integer}").expect("...");
}

```

This isn't possible to do without `std` and `std::io::Write`, despite there being an equivalent in `core::fmt::Write`.

This was quite suprising to me, you can workaround it by doing an extension trait on `&mut [u8]`, but this seems a bit unnecessary.

Is there a reason `&mut [u8]` doesn't implement `core::fmt::Write` ?

---

<div class="post-metadata">

**Author:** ![chrefr](https://avatars.discourse-cdn.com/v4/letter/c/e480ec/32.png) [@chrefr](https://internals.rust-lang.org/u/chrefr)\
**Post date:** [August 12, 2025, 10:04am UTC](https://internals.rust-lang.org/t/mut-u8-and-core-write/23369/2 "2025-08-12T10:04:12Z")

</div>

While you can use `core::fmt::Write` on byte slices, it is fundamentally for strings (it will also reject non-valid Unicode AFAIK). `std::io::Write` is for byte slices.

If we had a stack-string ([ArrayString in arrayvec - Rust](https://docs.rs/arrayvec/latest/arrayvec/struct.ArrayString.html)) in std, it would probably implement `core::fmt::Write`. But we don't.

---

<div class="post-metadata">

**Author:** ![ais523](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@ais523](https://internals.rust-lang.org/u/ais523)\
**Post date:** [August 12, 2025, 11:44am UTC](https://internals.rust-lang.org/t/mut-u8-and-core-write/23369/3 "2025-08-12T11:44:00Z")

</div>

I guess the "correct" API for this sort of thing would be to write into an `&mut [u8]` or even `&mut [MaybeUninit<u8>]`, producing an `&mut str` (reborrowed from the `&mut [u8]`) that referred to just the written potion – that way you have a reference that knows it's pointing to UTF-8, pointing at the existing storage. But that isn't very compatible with the way that the `Write` traits work today.

The problem with doing an API like the one suggested in the original post is that it loses information about how much of the input has been written (and thus how much is valid UTF-8).
