liveBook · Manning

Array

As explained in Learn Rust in a Month of Lunches

An array is a fixed-size Rust collection whose length is known at compile time and whose elements have one type.

An array holds several values in one place, in order. Create one by putting comma-separated values in square brackets: `let numbers = [0, 10, -20];`. Every element must have the same type, and the array cannot grow or shrink. section 3.1 section 3.1.1

An array’s type includes both its element type and its length: `["One", "Two"]` has type `[&str; 2]`, while `["One"]` has type `[&str; 1]`. Those are different types, even though both hold `&str` values. Rust knows an array’s length at compile time. section 3.1.1 section 3.1.2

Repeat a value

Use `[value; count]` when every position should start the same way: `let mut buffer = [0u8; 640];` creates 640 zero-valued bytes. Because `buffer` is mutable, its elements can change as data arrives—but it still holds exactly 640 `u8` values. Actual buffer operations are left for later. section 3.1.1

Use square brackets to get an element: for `let numbers = [0, 10, -20];`, `numbers[1]` is `10`. A range can select a slice, or view, of part of an array. For example, `&numbers[1..]` selects the values from position 1 onward. section 3.1.1

fn main() {
    let array_of_ten = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
 
    let two_to_five = &array_of_ten[2..5];     #1
    let start_at_one = &array_of_ten[1..];     #2
    let end_at_five = &array_of_ten[..5];      #3
    let everything = &array_of_ten[..];        #4
 
    println!("Two to five: {two_to_five:?},
Start at one: {start_at_one:?},
End at five: {end_at_five:?},
Everything: {everything:?}");
}

Compare the four range forms: a bounded portion, everything after a starting position, everything before an ending position, and the whole array. section 3.1.1

Choose an array when a fixed size suits the job; choose a vector when the collection needs to grow. Arrays can also be used directly in `for` loops, destructured into variables, and transformed with `.map()` into another array of the same length. Const generics let a type work with arrays of different lengths instead of requiring a separate definition for each length. section 3.1.2 section 16.1 section 20.1.1 section 20.1.2

How Learn Rust in a Month of Lunches covers it

How deep it goes
Works through creation, types, repetition, indexing, and slices, but defers actual byte-buffer operations.
Examples
Shows string arrays, a repeated-zero byte buffer, indexed numbers, range slices, and mapped integers.
What it assumes
Introduces array rules from the beginning while using basic Rust variables and familiar element types in examples.
What you can do after
Build it from scratch with bracket or repetition syntax, then index, slice, and change elements without changing the array’s size.
How it connects
Connects arrays to vectors, slices, iteration, destructuring, mapping, and const generics.

Also explained in

Compare how 40 books explain array →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.