liveBook · Manning

Slicing

As explained in Rust in Action: Systems programming concepts and techniques

A slice is a dynamically sized, fixed-length view of contiguous elements that does not own or resize the underlying storage.

A slice is a fixed-length view of contiguous elements. Its length need not be known at compile time, but the slice does not grow or shrink. A view can provide access to existing data without copying it. section 2.10.2

The notation marks an important distinction: `[T; n]` is an array whose length is part of its type; `[T]` is a slice without a compile-time length. Slices usually appear through references such as `&[T]`. That reference holds a pointer and a length, giving Rust a known-size value to work with. section 2.10.1 section 2.10.2

Why bother? Arrays of different lengths have different types, while a slice lets code work with a view without being tied to one array length. Text has a related form: `&str` is called a string slice. Unlike a growable `String`, a `str` cannot expand or shrink. section 2.10.2 section 2.9

fn main() {
  let mut memory: [u8; 4096] = [0; 4096];
  let mem = &mut memory;
 
  let add_twice = [
    0x80, 0x14,
    0x80, 0x14,
    0x00, 0xEE,
  ];
 
  mem[0x100..0x106].copy_from_slice(&add_twice);
 
  println!("{:?}", &mem[0x100..0x106]);     #1
}
fn main() {
  let mut memory: [u8; 4096] = [0; 4096];
  let mem = &mut memory;
 
  mem[0x100] = 0x80; mem[0x101] = 0x14;
  mem[0x102] = 0x80; mem[0x103] = 0x14;
  mem[0x104] = 0x00; mem[0x105] = 0xEE;
 
  println!("{:?}", &mem[0x100..0x106]);    #1
}

The range `mem[0x100..0x106]` selects part of a byte array. `copy_from_slice(&add_twice)` copies six opcode bytes into that selected region; the following line prints the same region. section 5.7.4

Slices are useful when choosing how to handle data, not just how to write a type. For text files that may contain invalid UTF-8, reading bytes as `[u8]` and decoding them with knowledge of the data is more robust than assuming the whole file is a `String`. section 2.10.3

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Defines fixed-length, dynamically sized slices and explains referenced pointer-and-length representation, without a broad treatment of slicing operations.
Examples
Shows a byte-array range receiving opcodes through `copy_from_slice()` and discusses `[u8]` for potentially invalid text input.
What it assumes
Builds from arrays and basic text handling, explaining the array–slice type distinction rather than assuming it.
What you can do after
Apply it via `copy_from_slice()` to copy bytes into a selected array range.
How it connects
Connects slices to arrays, traits, string slices, vectors, and UTF-8 decoding.

Also explained in

Compare how 16 books explain slicing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.