Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

str is Rust’s primitive string slice type, representing a dynamically sized, contiguous sequence of valid UTF-8 bytes. As a Dynamically Sized Type (DST), the exact memory footprint of a str is unknown at compile time. Consequently, it is almost exclusively accessed behind a reference as &str, which acts as a “fat pointer” containing both the memory address and the length of the slice.

Memory Layout

A &str consists of two machine words:
  1. A pointer to the first byte of the string data.
  2. A usize representing the length of the string in bytes (not characters).
// Conceptual representation of &str under the hood
pub struct StringSlice {
    ptr: *const u8,
    len: usize,
}

UTF-8 Enforcement

Rust guarantees at the type-system level that every str contains strictly valid UTF-8. Because UTF-8 is a variable-width encoding (where a single Unicode scalar value can occupy 1 to 4 bytes), str does not support direct constant-time index operations (e.g., my_str[2]). Indexing into a str requires slicing via byte ranges. The standard library enforces UTF-8 validity during slicing; if a byte range boundary splits a multi-byte character, the program will panic at runtime.
let text: &str = "Rüst"; // 'ü' occupies 2 bytes

// Valid: Slicing exactly on character boundaries
let slice_r: &str = &text[0..1]; // "R"
let slice_u: &str = &text[1..3]; // "ü"

// Invalid: Panics at runtime for splitting a UTF-8 character boundary
// let panic_slice = &text[1..2]; 

Instantiation and Lifetimes

A &str can point to memory located anywhere: the heap, the stack, or static memory. When defined as a string literal, the &str points to pre-allocated, read-only memory embedded directly in the compiled binary (typically the .rodata section). These literals inherently possess a 'static lifetime.
// String literal with a static lifetime
let literal: &'static str = "Embedded in the binary";
A &str is also commonly generated by borrowing a slice of an owned String. In this scenario, the &str points to a segment of heap-allocated memory, and the borrow checker binds its lifetime to the scope of the underlying String.
let owned_string: String = String::from("Heap allocated data");

// Coercing the entire String into a &str (Deref coercion)
let full_slice: &str = &owned_string; 

// Slicing a specific byte range into a &str
let partial_slice: &str = &owned_string[0..4]; 

Mutability (&mut str)

While &str is an immutable reference, Rust does expose mutable string slices (&mut str). However, because a str is a DST whose size cannot be altered, and because the UTF-8 guarantee must remain intact, operations on a &mut str are highly restricted. You cannot append, remove, or replace characters with differently-sized characters. You can only perform in-place byte mutations that preserve the exact byte length and maintain valid UTF-8 encoding.
let mut owned = String::from("ascii");
let mut_slice: &mut str = &mut owned[..];

// Valid: Modifies bytes in-place without changing length or violating UTF-8
mut_slice.make_ascii_uppercase(); 
Master Rust with Deep Grasping Methodology!Learn More