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
/// Returns the index at which the first occurrence of a element is found in the collection.
///
///
/// # Arguments
///
/// * `collection` - The collection iterate over.
///
/// * `item` - The element to find.
///
/// # Returns
///
/// Index of first occurrence of the given element.
///
/// # Examples
///
/// ```
/// use rufl::collection;
///
/// assert_eq!(None, collection::index_of(&[1, 2, 3], &0));
///
/// assert_eq!(Some(1), collection::index_of(&[1, 2, 3, 2], &2));
///
/// ```
pub fn index_of<C: AsRef<[T]>, T: PartialEq + Clone>(collection: &C, item: &T) -> Option<usize> {
for (i, v) in collection.as_ref().to_vec().iter().enumerate() {
if *v == *item {
return Some(i);
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_index_of() {
assert_eq!(None, index_of(&[1, 2, 3], &0));
assert_eq!(Some(0), index_of(&[1, 2, 3], &1));
assert_eq!(Some(1), index_of(&[1, 2, 3, 2], &2));
}
}