#sorting #set #compare #maintain #vector-backed #sorted-set

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

#148 in Data structures

Download history 26880/week @ 2025-09-30 54664/week @ 2025-10-07 70144/week @ 2025-10-14 67575/week @ 2025-10-21 66000/week @ 2025-10-28 60724/week @ 2025-11-04 62635/week @ 2025-11-11 83663/week @ 2025-11-18 44007/week @ 2025-11-25 72039/week @ 2025-12-02 67456/week @ 2025-12-09 69344/week @ 2025-12-16 32897/week @ 2025-12-23 36720/week @ 2025-12-30 85043/week @ 2026-01-06 87923/week @ 2026-01-13

250,102 downloads per month
Used in 129 crates (28 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

~160KB