expression

Rust Index arrays and slices

Indexing selects one element by its zero-based position. For array and slice indexing, use a `usize` index. An invalid runtime index panics; `get` offers an optional result. Expected output (not executed): 20 Why: Position 1 refers to the second element. Common mistake: Do not index at `len()`; that position is past the last element. A statically obvious invalid index may be rejected at compile time.

Formal Syntax & Structure

rust syntaxrust
array[index]

Working Example

Example Snippetrust
fn main() {    let scores = [10, 20, 30];    let index = 1_usize;    println!("{}", scores[index]);}
Rust Reference