Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Rust

Idiom #335 List to map

Create the map m containing all the elements e of the list a, using as key the field e.id.

use std::collections::HashMap;
let mut m = HashMap::new();

for e in a {
	m.entry(e.id).or_insert_with(Vec::new).push(e);
}
var m = a.ToDictionary(e => e.id, e => e);

New implementation...
< >
programming-idioms.org