Let's say I have a &mut std::collections::HashMap
, and I want to turn all the keys into uppercase. The following code does the trick:
use std::collections::HashMap;
fn keys_to_upper<T>(map: &mut HashMap<String, T>) {
let mut tmp = Vec::with_capacity(map.len());
for (key, val) in map.drain() {
tmp.push((key.to_ascii_uppercase(), val));
}
for (key, val) in tmp {
map.insert(key, val);
}
}
Unfortunately, I don't have a HashMap
but a &mut serde_json::Map
, and I want to turn all the keys into uppercase. There is no .drain()
method. I could use .into_iter()
instead, but that would only give me mutable references to the keys and values. To insert them into the map again I would have to clone them, which would hurt performance.
Is there some way here to get around the absense of the .drain()
method?