我想将一个
HashSet
用作 HashMap
的键。这可行吗?use std::collections::{HashMap, HashSet};
fn main() {
let hmap: HashMap<HashSet<usize>, String> = HashMap::new();
}
显示以下错误:
error[E0277]: the trait bound `std::collections::HashSet<usize>: std::hash::Hash` is not satisfied
--> src/main.rs:4:49
|
4 | let hmap: HashMap<HashSet<usize>, String> = HashMap::new();
| ^^^^^^^^^^^^ the trait `std::hash::Hash` is not implemented for `std::collections::HashSet<usize>`
|
= note: required by `<std::collections::HashMap<K, V>>::new`
BTreeSet
作为HashMap
的键,因为BTreeSet
实现了适当的特性。我知道这不是原问题的严格答案,但它似乎仍在范围内:原问题正是我在寻找的,但对我来说,BTreeSet
是一个完全可以接受的答案。 - Danek Duvall