> ## 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.

# Rust Slice

A slice in Rust is a dynamically-sized type (DST) representing a contiguous view into a block of memory. It allows safe, borrow-checked access to a portion of a collection—such as an array, `Vec<T>`, or `String`—without copying the underlying data.

Because the slice itself (`[T]`) is dynamically sized, its size is not known at compile time. Consequently, slices are almost exclusively interacted with via references (`&[T]` for shared access or `&mut [T]` for mutable access).

Under the hood, a slice reference is a "fat pointer" consisting of exactly two machine words:

1. A pointer to the memory address of the first element in the slice.
2. A `usize` representing the length of the slice (the number of elements it contains).

## Syntax and Instantiation

Slices are constructed using the borrowing operator (`&` or `&mut`) combined with Rust's range syntax (`start..end`), where `start` is inclusive and `end` is exclusive.

```rust theme={"dark"}
let array: [i32; 5] = [10, 20, 30, 40, 50];

// Shared slice reference (&[i32])
let slice: &[i32] = &array[1..4]; // Contains [20, 30, 40]

// Mutable slice reference (&mut [i32])
let mut mut_array: [i32; 5] = [10, 20, 30, 40, 50];
let mut_slice: &mut [i32] = &mut mut_array[1..4];
```

Rust provides syntactic sugar for omitting bounds when slicing from the beginning or to the end of a collection:

```rust theme={"dark"}
let collection = [0, 1, 2, 3, 4];

let a = &collection[..];    // Full range: index 0 to len
let b = &collection[2..];   // From index 2 to len
let c = &collection[..3];   // From index 0 to 3 (exclusive)
let d = &collection[1..=3]; // Inclusive range: index 1 to 3 (inclusive)
```

## String Slices (`&str`)

A string slice (`&str`) is a specialized, primitive slice type. While `&[T]` is a slice of arbitrary elements, `&str` is fundamentally a slice of bytes (`&[u8]`) with a strict type system and standard library guarantee that the underlying byte sequence is valid UTF-8.

```rust theme={"dark"}
let heap_string: String = String::from("Compiler");
let str_slice: &str = &heap_string[0..4]; // "Comp"

// String literals are inherently string slices pointing to static memory
let literal: &'static str = "Static data"; 
```

*Note: Slicing a string must occur at valid UTF-8 character boundaries. Slicing in the middle of a multi-byte character will cause a runtime panic.*

## Type System Mechanics

* **`[T]`**: The slice type itself. It does not implement the `Sized` trait (making it an unsized type), meaning it cannot be stored directly on the stack or passed by value.
* **`&[T]`**: The slice reference. It implements the `Sized` trait because the fat pointer has a fixed, known size at compile time (16 bytes on a 64-bit architecture).
* **Deref Coercion**: Collections that manage contiguous memory implement the `Deref` trait targeting their respective slice types. `Vec<T>` implements `Deref<Target = [T]>`, and `String` implements `Deref<Target = str>`. This allows a `&Vec<T>` to implicitly coerce into a `&[T]` when passed to functions or methods.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Rust Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
