#sorting #vector #compare #set #maintain

sorted-vec

Create and maintain sorted vectors and vector-backed sets

25 releases

Uses new Rust 2024

0.8.10 Sep 5, 2025
0.8.8 Aug 15, 2025
0.8.7 Jul 27, 2025
0.8.6 Jan 25, 2025
0.3.1 Mar 19, 2019

#11 in #maintain

Download history 16360/week @ 2026-03-03 114507/week @ 2026-03-10 96527/week @ 2026-03-17 110569/week @ 2026-03-24 100779/week @ 2026-03-31 125337/week @ 2026-04-07 116801/week @ 2026-04-14 109746/week @ 2026-04-21 117002/week @ 2026-04-28 109472/week @ 2026-05-05 137767/week @ 2026-05-12 114563/week @ 2026-05-19 109643/week @ 2026-05-26 113417/week @ 2026-06-02

494,255 downloads per month
Used in 161 crates (30 directly)

Apache-2.0

64KB
1.5K SLoC

Sorted vectors.

Repository

  • SortedVec -- sorted from least to greatest, may contain duplicates
  • SortedSet -- sorted from least to greatest, unique elements
  • ReverseSortedVec -- sorted from greatest to least, may contain duplicates
  • ReverseSortedSet -- sorted from greatest to least, unique elements

The partial module provides sorted vectors of types that only implement PartialOrd where comparison of incomparable elements results in runtime panic.


sorted_vec

Create and maintain collections of sorted elements.

Documentation

let mut v = SortedVec::new();
assert_eq!(v.insert (5), 0);
assert_eq!(v.insert (3), 0);
assert_eq!(v.insert (4), 1);
assert_eq!(v.insert (4), 1);
assert_eq!(v.len(), 4);
v.dedup();
assert_eq!(v.len(), 3);
assert_eq!(v.binary_search (&3), Ok (0));
assert_eq!(*SortedVec::from_unsorted (
  vec![5, -10, 99, -11, 2, 17, 10]),
  vec![-11, -10, 2, 5, 10, 17, 99]);

Also provides sorted set containers only containing unique elements.

serde support

serde de/serialization is an optional feature.

By default, deserializing an unsorted container is an error.

To sort on deserialization, tag the field with #[serde(deserialize_with = "SortedVec::deserialize_unsorted")]:

#[derive(Debug, Eq, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
pub struct Foo {
  #[serde(deserialize_with = "SortedVec::deserialize_unsorted")]
  pub v : SortedVec <u64>
}

Dependencies

~140KB