Macro thetadb::search

source ·
macro_rules! search {
    ($key:expr, $len:expr, $index:ident => $obtain:expr) => { ... };
}
Expand description

Generates code for a binary search algorithm that searches for a given key in a collection.

Returns the index of the key if found, or and error indicating where the key should be inserted to maintain a sorted order.

§Arguments

  • key - The value you’re searching for in the collection.
  • len - The length of the collection.
  • obtain - An expression that takes an index and returns the value at that index in the collection.

§Examples

use thetadb::search;

let slice = &[0, 2, 4, 6, 8];

assert_eq!(
    Ok(1),
    search!(2, slice.len(), idx => slice[idx]),
);
assert_eq!(
    Err(2),
    search!(3, slice.len(), idx => slice[idx]),
);