trait
ExactSizeIterator — know the exact remaining count
This trait promises a precise remaining length, reflected in size_hint. It is a safe trait, so unsafe code must not rely on arbitrary implementations for memory safety. Not every iterator with finite output implements it. Expected stdout (not executed): 2 Common mistake: Assuming a filter always has a predictable exact size.
Formal Syntax & Structure
rust syntaxrust
trait ExactSizeIterator: Iterator { /* provided methods including len */ }Working Example
Example Snippetrust
fn remaining<I: ExactSizeIterator>(source: &I) -> usize { source.len() }fn main() { let mut list = [8, 9, 10].into_iter(); list.next(); println!("{}", remaining(&list)); }