← LeaderboardThe Skill Leaderboard

rust-slice-duplicate-key-ranges

Find every duplicate key or a bounded window in a Rust sorted slice. Use partition_point to preserve interval boundaries and verify equal-key order.

Published by sickn33 ↗sickn33/agentic-awesome-skills

Try your first task

A source-grounded starter task has not been verified for this skill yet.

SKILL.md

name: rust-slice-duplicate-key-rangesdescription: Find every duplicate key or a bounded window in a Rust sorted slice. Use partition_point to preserve interval boundaries and verify equal-key order.category: developmentrisk: criticalsource: selfsource_type: selfdate_added: "2026-12-03"author: ShianMiketags: [rust, slice, binary-search, partition-point, bounds]

Rust sorted-slice duplicate-key ranges

When to Use

  • A Rust lookup on a sorted slice returns only one of several records with the same key.
  • A time or key window drops events at its boundary after switching to binary search.
  • The caller needs a borrowed range with all matching records in the slice's existing order.

Prerequisites

  • Identify the field and comparison used to build the sorted slice. Locate every path that inserts, sorts, or mutates its keys.
  • Confirm the caller expects a single hit, all equal keys, or a range with specific inclusive and exclusive bounds.
  • Use a fixture with distinct payloads at the same key so it can show which records disappear.

1. Check the order and range contract

  1. Trace from indexing to the public lookup. binary_search_by_key returns one match on success. When keys repeat, that index can be any of the equal records.
  2. Confirm the slice is sorted in the same direction and by the same key the lookup uses. A partition_point predicate assumes a prefix of true results followed by false results.
  3. Identify how ties are ordered during index construction. Taking a slice preserves the order already present. If equal-key source order matters, preserve it when populating or sorting the input.
  4. Verify those properties at the sorted-data boundary with tests or assertions where appropriate. A full sortedness scan inside a frequent lookup adds linear work.

2. Find both boundaries

  • For an ascending equal-key lookup, the first partition point is the end of the keys less than the target. The second is the end of the keys less than or equal to it.
  • Return the slice between those positions. Missing keys give equal positions and an empty slice, including before the first and after the last record.
  • For an ascending interval, use a predicate for each side. A lower inclusive bound uses < lower and a lower exclusive bound uses <= lower. An upper inclusive bound uses <= upper and an upper exclusive bound uses < upper. Check the caller's contract for empty or inverted intervals before slicing.
  • Compare to the key itself. Computing a successor such as key + 1 can overflow at a numeric maximum and may have no equivalent for strings or other ordered types.

3. Verify payloads at the boundaries

  1. Assert the full sequence for a repeated key and include distinct records immediately outside the expected range.
  2. Check empty input, unique keys, misses on both sides and between records, and an extreme value.
  3. If a public API supports both inclusive and exclusive intervals, exercise each endpoint and check the intended behavior for reversed bounds.
  4. Keep a linear filter in a targeted regression test when the index has a custom comparator or composite key. Compare payload sequences as well as counts.

Example

Find every event at a tick reproduces the missing duplicates when a single binary-search hit becomes a one-element slice. Two partition points recover all matching events, including repeated maximum keys.

Limitations

  • The example uses a fixed ascending integer key and exact-match ranges. It leaves descending order, floating-point policies, and application-specific interval types to their own fixtures.
  • A borrowed range is valid while the backing slice remains borrowed. Consumers that hold results across mutations may need owned values.
  • Binary partitioning depends on monotonic predicates. Use the ordering actually used to construct the index.

Safety and Side Effects

The example tests synthetic records in memory. If production sorting changes, evaluate the effect on equal-key order and any consumer that stores positions into the slice.

Related Skills and Documentation