std-method

array::each_ref

Borrow every array element and collect the references in an array of the same length. The original array remains owned by its binding. Expected stdout (not executed): [3, 3] Ada Common mistake: Consuming an array of String values just to inspect each one.

Formal Syntax & Structure

rust syntaxrust
array.each_ref() -> [&T; N]

Working Example

Example Snippetrust
fn main() {    let names = [String::from("Ada"), String::from("Lin")];    let sizes = names.each_ref().map(|s| s.len());    println!("{sizes:?}");    println!("{}", names[0]);}
Rust Reference