Semantic of `Map/Set::from_iter` with duplicated keys

⚓ rust    📅 2025-06-07    👤 surdeus    👁️ 3      

surdeus

I couldn't find much documentation on HashMap::from_iter (or BTreeMap, for that matter). What is the semantic around duplicates keys?

Based on my testing (code below), it seems that HashMap uses the same semantic as applying insert from the front, i.e., use the first duplicated keys and the last duplicated values. Is this a guaranteed behavior? BTreeMap seems to have a different semantic where the last duplicated keys are used instead.

let keys = [0, 1, 2, 3, 1, 2];
let values = [0, 1, 2, 3, 4, 5];
let hash_map = std::collections::HashMap::<_, _>::from_iter(std::iter::zip(&keys, values));
let mut used_keys_idx = Vec::from_iter(
    hash_map
        .keys()
        // SAFETY: well-aligned from the same allocated object (`keys`)
        .map(|k| unsafe { std::ptr::from_ref(*k).offset_from(keys.as_ptr()) }),
);
let mut used_values = Vec::from_iter(hash_map.values());
used_keys_idx.sort();
used_values.sort();
println!("{used_keys_idx:?} {used_values:?}"); // [0, 1, 2, 3] [0, 3, 4, 5]

2 posts - 2 participants

Read full topic

🏷️ rust_feed