我有两个 HashMap
,想要计算它们的键的交集。是否可以通过 HashMap::keys()
返回的内容构建一个 HashSet
呢?例如:
use std::collections::{HashSet, HashMap};
fn main() {
let mut map1: HashMap<i64, i64> = HashMap::new();
let mut map2: HashMap<i64, i64> = HashMap::new();
// Add some values into the HashMaps for demonstration
map1.insert(1, 10);
map1.insert(5, 50);
map2.insert(3, 30);
map2.insert(5, 50);
let set1: HashSet<i64> = HashSet::from(map1.keys()); // How to do this?
let set2: HashSet<i64> = HashSet::from(map2.keys()); // How to do this?
let set3 = set1.intersection(&set2); // What I'm looking to accomplish
// set3 should contain [5], as this is the one key shared by the two HashMaps
}