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















