RS.CLIPPY.TRANSMUTE_BYTES_TO_STR

Transmutes from a `&[u8]` to a `&str`

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: transmute_bytes_to_str. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks for transmutes from a &[u8] to a &str.

Why is this bad?

Not every byte slice is a valid UTF-8 string.

Known problems

  • from_utf8 which this lint suggests using is slower than transmute as it needs to validate the input. If you are certain that the input is always a valid UTF-8, use from_utf8_unchecked which is as fast as transmute but has a semantically meaningful name.
  • You might want to handle errors returned from from_utf8 instead of calling unwrap.

Example

let b: &[u8] = &[1_u8, 2_u8];
unsafe {
    let _: &str = std::mem::transmute(b); // where b: &[u8]
}

// should be:
let _ = std::str::from_utf8(b).unwrap();