[−][src]Trait spans::Spans
Spans
provides an iterator adapter for SpansBy
.
Provided methods
pub fn spans_by_key<K, C, F>(
self,
key: K,
are_connected: F
) -> SpansBy<Self, K, F> where
K: Fn(&Self::Item) -> C,
C: Copy,
F: Fn(C, C) -> bool,
Self: Sized,
[src]
self,
key: K,
are_connected: F
) -> SpansBy<Self, K, F> where
K: Fn(&Self::Item) -> C,
C: Copy,
F: Fn(C, C) -> bool,
Self: Sized,
Splits the iterator into contiguous spans.
are_connected
returns true
if the two given adjacent items are part of the same span, false
otherwise.
Items are not compared directly; instead, a key is made for each item using the key
function.
are_connected
is given the key of the previous item and the key of the current item.
Examples
Create spans for items increasing in value by 1:
use spans::Spans; let vec = vec![1, 2, 5, 6, 7, 11, 13, 14, 15]; let mut spans = vec.iter().spans_by_key(|&&x| x, |a, b| a + 1 == b); assert_eq!(spans.next().unwrap().collect::<Vec<_>>(), vec![&1, &2]); assert_eq!(spans.next().unwrap().collect::<Vec<_>>(), vec![&5, &6, &7]); assert_eq!(spans.next().unwrap().collect::<Vec<_>>(), vec![&11]); assert_eq!(spans.next().unwrap().collect::<Vec<_>>(), vec![&13, &14, &15]); assert!(spans.next().is_none());
Create spans for strings of the same length:
use spans::Spans; let vec = vec!["abc", "run", "tag", "go", "be", "ring", "zip", "zap", "put"]; let mut spans = vec.iter().spans_by_key(|x| x.len(), |a, b| a == b); assert_eq!( spans.next().unwrap().collect::<Vec<_>>(), vec![&"abc", &"run", &"tag"] ); assert_eq!( spans.next().unwrap().collect::<Vec<_>>(), vec![&"go", &"be"] ); assert_eq!( spans.next().unwrap().collect::<Vec<_>>(), vec![&"ring"] ); assert_eq!( spans.next().unwrap().collect::<Vec<_>>(), vec![&"zip", &"zap", &"put"] ); assert!(spans.next().is_none());