Under heavy development. This site describes the intended 1.0 language. Current features and performance vary. Development status
Sn50SnThe SnAPI programming language
Development · Evolving reference

CallableBinarySearch

Finds the first equal element or lower-bound insertion position with a concrete comparator.

Sn is under heavy development. This reference includes the intended 1.0 design; features may be incomplete in the current compiler. Read the status →

Module std.iteration

import std.iteration;

Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.

BinarySearch

pub fn BinarySearch<T, C>(Values: readonly T[], Key: readonly T&,
    mut Compare: C) -> Result<usize, usize>
    where C: BinaryCallable<readonly T&, readonly T&, i32>, Movable

Finds the first equal element or lower-bound insertion position with a concrete comparator.

Owns the concrete callback during O(log n) borrowed comparisons. Uses O(1) state and introduces no allocation, element copies/moves, mutation or lock. Captured backing and sorted storage remain live and unchanged throughout the call. Duplicates return the first matching index; empty input inserts at zero. T: Exact stored element type, including non-Copy owners. C: Movable safe readonly binary comparator, inferred from Compare. Values: Readonly contiguous storage sorted by this same ordering. Key: Borrowed target, not retained in the result. Compare: Owned concrete comparator; non-Copy named state requires move. Return: Ok first matching index or Err insertion position in [0,Length]. Pre: Compare supplies a consistent ordering and never invalidates Values or Key. Warning: No linear ordering validation occurs; callback/backing costs remain their contracts.

Documentation snapshot: October 24, 2026 About versions ↗
Searching Development · language & standard library