Function in collections::vec coil-stdlib

index_of

fn index_of(Vec<int> xs, int needle) -> int
fn index_of(Vec<string> xs, string needle) -> int
Import
use collections::vec::{index_of};
Source: src/collections/vec.hy

Returns the index of the first needle in xs, or -1. There is an int and a string version.

fn index_of(Vec<int> xs, int needle) -> int#

Parameters#

NameType
xsVec<int>
needleint

Returns#

int

Contracts#

Checked according to --contracts (see Contracts).

ensures result >= -1 && result < xs.len()
ensures result < 0 || xs[result] == needle

fn index_of(Vec<string> xs, string needle) -> int#

Returns the index of the first needle in xs, or -1.

Parameters#

NameType
xsVec<string>
needlestring

Returns#

int

Contracts#

Checked according to --contracts (see Contracts).

ensures result >= -1 && result < xs.len()
ensures result < 0 || xs[result] == needle

Examples#

use collections::vec::{index_of};
use tests::assert;
let xs: Vec<int> = [3, 1, 4, 1];
assert!(index_of(xs, 1) == 1);
assert!(index_of(xs, 9) == -1);

See also#

  • int
  • string
  • fold_int: The same as fold; kept for older code.
  • contains: Returns true when needle is in xs.
  • binary_search: Finds needle in the ascending vector xs by halving, and returns an index holding it.
  • dedup: Returns xs with runs of equal neighbours collapsed to one value.
  • map: Returns a new vector with mapper applied to each element, in order.
  • filter: Returns the elements for which pred is true, in order.
↑ ↓ navigate ↵ open