是否有保留插入顺序的集合类型?

tsh*_*ang 4 rust

是否存在保留插入顺序(认为Vec)但仅跟踪唯一值(认为HashSet)的类型?我想避免使用,Vec因为在插入之前我首先需要检查该值是否存在。

Ale*_*ler 5

linked_hash_set板条箱现在可用。它基于尽可能接近linked-hash-mapstd HashSetAPI 的 板条箱。

extern crate linked_hash_set;
use linked_hash_set::LinkedHashSet;

let mut set = LinkedHashSet::new();
set.insert(234);
set.insert(123);
set.insert(345);
set.insert(123);

assert_eq!(set.into_iter().collect::<Vec<_>>(), vec![234, 345, 123]);
Run Code Online (Sandbox Code Playgroud)


小智 5

linked-hash-map由于处于维护模式,因此正在更新答案(请参阅github commit


2021 年 9 月最受欢迎的 github crate:

  • indexmap提供IndexSet.
  • array_tool提供了用几行代码构建向量集数据结构的帮助器。