1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/// Finds the position of the first occurrence of an element in a collection.
/// Returns -1 if the element is not found.
///
/// # Arguments
/// * `collection` - A slice of items.
/// * `element` - The element to search for.
///
/// # Returns
/// * `isize` - The index of the first occurrence of the element, or -1 if not found.
///
/// # Examples
/// ```rust
/// use lowdash::index_of;
/// let collection = vec![1, 2, 3, 4, 5];
/// let index = index_of(&collection, 3);
/// assert_eq!(index, 2);
/// ```
///
/// ```rust
/// use lowdash::index_of;
/// let collection = vec!["apple", "banana", "cherry"];
/// let index = index_of(&collection, "banana");
/// assert_eq!(index, 1);
/// ```
///
/// ```rust
/// use lowdash::index_of;
/// let collection = vec![1, 2, 3, 4, 5];
/// let index = index_of(&collection, 6);
/// assert_eq!(index, -1);
/// ```
pub fn index_of<T: PartialEq>(collection: &[T], element: T) -> isize {
for (i, item) in collection.iter().enumerate() {
if *item == element {
return i as isize;
}
}
-1
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_index_of_found() {
let collection = vec![1, 2, 3, 4, 5];
assert_eq!(index_of(&collection, 3), 2);
}
#[test]
fn test_index_of_not_found() {
let collection = vec![1, 2, 3, 4, 5];
assert_eq!(index_of(&collection, 6), -1);
}
#[test]
fn test_index_of_empty_collection() {
let collection: Vec<i32> = vec![];
assert_eq!(index_of(&collection, 1), -1);
}
#[test]
fn test_index_of_strings() {
let collection = vec!["apple", "banana", "cherry"];
assert_eq!(index_of(&collection, "banana"), 1);
assert_eq!(index_of(&collection, "orange"), -1);
}
#[test]
fn test_index_of_floats() {
let collection = vec![1.1, 2.2, 3.3, 4.4, 5.5];
assert_eq!(index_of(&collection, 3.3), 2);
assert_eq!(index_of(&collection, 6.6), -1);
}
}