std-method

slice::as_flattened

Borrow nested fixed-length rows as one contiguous slice without building a new vector. It preserves row-major element order. Expected stdout (not executed): [1, 2, 3, 4] Common mistake: Trying to flatten a vector of separately allocated vectors this way.

Formal Syntax & Structure

rust syntaxrust
slice_of_arrays.as_flattened()

Working Example

Example Snippetrust
fn main() {    let grid = [[1, 2], [3, 4]];    let flat: &[i32] = grid.as_flattened();    println!("{flat:?}");}
Rust Reference