首页 rust

use peek_nth::{IteratorExt, PeekableNth};
use std::slice::Iter;

fn main() {
    let a = vec![1, 2, 3];
    let mut b = a.iter().peekable_nth();
    println!("{:?}", b.peek());
    println!("{:?}", b.next());
    println!("{:?}", b.peek());
    print_peek(&mut a.iter().peekable_nth());
    println!("Hello, world!");
}

fn print_peek(peek: &mut PeekableNth<Iter<i32>>) {
    println!("{:?}", peek.peek());
    println!("{:?}", peek.next());
    println!("{:?}", peek.peek());
}
Some(1)
Some(1)      
Some(2)      
Some(1)      
Some(1)      
Some(2)      
Hello, world!

补充:
peek_nth(num) 中num为大于等于零的数字,可以直接查看迭代器中的第num+1位元素,但是没有执行next,所以如果之后还要操作,要进行next操作




文章评论