expression

Rust Initialize arrays with repeated values

Use repetition syntax when a fixed-size array begins with identical elements. This example repeats a copyable integer; non-Copy values have additional restrictions. Expected output (not executed): [0, 0, 1, 0] Why: Four zero values are created and one slot is updated. Common mistake: The count is a compile-time constant, not a runtime length like a vector.

Formal Syntax & Structure

rust syntaxrust
[value; count]

Working Example

Example Snippetrust
fn main() {    let mut seats = [0_u8; 4];    seats[2] = 1;    println!("{seats:?}");}
Rust Reference