Does anyone know of an Ordered Set implementation for Rust


#1

I’m trying to find an implementation of a set that maintains its insertion order when iterated. In my particular case it’s needed for encoding a preference for a set of values. I could abuse a BTreeMap, I suppose, but it’d be nicer to not. Does anyone know of an implementation?


#2

You’re looking for https://crates.io/crates/ordermap


#3

Ta. Several searches of crates.io didn’t find that…


#4

Note that removal perturbs insertion order with ordermap - https://crates.io/crates/linked-hash-map is another option that avoids that caveat if it matters to you.


#5

@sfackler Ta. Nice to know there’s two alternatives. In the last 10 years or so, I’ve only very occasionally needed remove on a map or set.

@arthurprs I’ve used OrderMap now and it does the ticket very nicely. Sadly, I’ve now got to write custom serde serialization logic for the places that use it, but that’s life…


#6

@raphcohn would you be able to submit a PR to the ordermap crate with the Serde impls you wrote? I needed it today as well. Serde could be an optional dependency of ordermap.


#7

My impls aren’t performant enough - they cheat by converting to and from other types that Serde already supports.