std-method

array::map

Transform a fixed-size array into another array of the same length, possibly with a different element type. The method takes the array by value. Expected stdout (not executed): ["$2", "$4", "$6"] Common mistake: Assuming map is lazy like Iterator::map. Array map returns the completed array.

Formal Syntax & Structure

rust syntaxrust
array.map(|element| result)

Working Example

Example Snippetrust
fn main() {    let prices = [2, 4, 6];    let labels = prices.map(|n| format!("${n}"));    println!("{labels:?}");}
Rust Reference