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