2 releases
new 0.1.1 | Jan 18, 2025 |
---|---|
0.1.0 | Jan 17, 2025 |
#573 in Rust patterns
37 downloads per month
15KB
149 lines
iter-index
crate
This is a simple extension trait that provides a more flexible alternative to the iterator's method enumerate()
. It allows to:
- use a custom type for the index with
index::<T>()
- define a custom start value with
index_start::<T>(start: T)
- define a custom step value with
index_step::<T>(start: T, step: T)
.
Examples
use iter_index::IndexerIterator;
let items = vec!["a", "b", "c"];
let mut result = items.into_iter().index::<i32>();
assert_eq!(result.next(), Some((0_i32, "a")));
assert_eq!(result.next(), Some((1_i32, "b")));
assert_eq!(result.next(), Some((2_i32, "c")));
assert_eq!(result.next(), None);
let items = vec!["a", "b", "c"];
let mut result = items.into_iter().index_start::<u8>(97);
assert_eq!(result.next(), Some((97_u8, "a")));
assert_eq!(result.next(), Some((98_u8, "b")));
assert_eq!(result.next(), Some((99_u8, "c")));
assert_eq!(result.next(), None);
let items = vec!["a", "b", "c"];
let mut result = items.into_iter().index_step::<u32>(100, 10);
assert_eq!(result.next(), Some((100_u32, "a")));
assert_eq!(result.next(), Some((110_u32, "b")));
assert_eq!(result.next(), Some((120_u32, "c")));
assert_eq!(result.next(), None);
License
This code is licensed under either MIT License or Apache License 2.0.