在向量中存储Peekable迭代器时,无法推断出适当的生命周期

时间:2017-07-10 02:56:21

标签: iterator rust lifetime

以下代码完美无缺:

use std::iter::Peekable;
use std::slice::Iter;

fn has_next(iter: &mut Peekable<Iter<usize>>) -> bool {
    match iter.peek() {
        Some(_) => true,
        None => false,
    }
}

fn print_iters(iters: &mut Vec<Peekable<Iter<usize>>>) {
    for iter in iters.iter_mut() {
        if has_next(iter) {
            match iter.next() {
                Some(x) => println!("{}", x),
                None => {}
            }
        }
    }
}

fn main() {
    let v1 = vec![2, 4, 6, 8];
    let v2 = vec![1, 3, 5, 7];
    let mut iters = Vec::new();
    iters.push((v1.iter().peekable()));
    iters.push((v2.iter().peekable()));
    print_iters(&mut iters);
}

在我编写的一些代码中,我需要将Peekable存储在一个向量中,并在以后使用它们进行迭代。我尝试将代码修改为:

use std::iter::Peekable;
use std::slice::Iter;

fn has_next(iter: &mut Peekable<Iter<usize>>) -> bool {
    match iter.peek() {
        Some(_) => true,
        None => false,
    }
}

fn print_iters(iters: &mut Vec<Peekable<Iter<usize>>>) {
    for iter in iters.iter_mut() {
        if has_next(iter) {
            match iter.next() {
                Some(x) => println!("{}", x),
                None => {}
            }
        }
    }
}

fn init_iters(v: &Vec<Vec<usize>>, iters: &mut Vec<Peekable<Iter<usize>>>) {
    for i in v.iter() {
        iters.push(i.iter().peekable());
    }
}

fn main() {
    let v1 = vec![2, 4, 6, 8];
    let v2 = vec![1, 3, 5, 7];
    let v = vec![v1, v2];
    let mut iters = Vec::new();
    init_iters(v, &mut iters);
    print_iters(&mut iters);
}

当我这样做时,我收到以下错误:

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
  --> src/main.rs:23:20
   |
23 |         for i in v.iter() {
   |                    ^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the body at 22:79...
  --> src/main.rs:22:80
   |
22 |       fn init_iters(v: &Vec<Vec<usize>>, iters: &mut Vec<Peekable<Iter<usize>>>) {
   |  ________________________________________________________________________________^
23 | |         for i in v.iter() {
24 | |             iters.push(i.iter().peekable());
25 | |         }
26 | |     }
   | |_____^
note: ...so that reference does not outlive borrowed content
  --> src/main.rs:23:18
   |
23 |         for i in v.iter() {
   |                  ^
note: but, the lifetime must be valid for the anonymous lifetime #3 defined on the body at 22:79...
  --> src/main.rs:22:80
   |
22 |       fn init_iters(v: &Vec<Vec<usize>>, iters: &mut Vec<Peekable<Iter<usize>>>) {
   |  ________________________________________________________________________________^
23 | |         for i in v.iter() {
24 | |             iters.push(i.iter().peekable());
25 | |         }
26 | |     }
   | |_____^
note: ...so that expression is assignable (expected std::iter::Peekable<std::slice::Iter<'_, _>>, found std::iter::Peekable<std::slice::Iter<'_, _>>)
  --> src/main.rs:24:24
   |
24 |             iters.push(i.iter().peekable());
   |                        ^^^^^^^^^^^^^^^^^^^

error[E0308]: mismatched types
  --> src/main.rs:33:20
   |
33 |         init_iters(v, &mut iters);
   |                    ^ expected reference, found struct `std::vec::Vec`
   |
   = note: expected type `&std::vec::Vec<std::vec::Vec<usize>>`
              found type `std::vec::Vec<std::vec::Vec<{integer}>>`
   = help: try with `&v`

为什么会出现此错误?我该如何解决?

1 个答案:

答案 0 :(得分:2)

根据continuations的规则,您的函数init_iters会扩展为

fn init_iters<'a, 'b, 'c>(v : &'a Vec<Vec<usize>>,
                          iters : &'b mut Vec<Peekable<Iter<'c, usize>>>) {
    for i in v.iter() {
        iters.push(i.iter().peekable());
    }
}

向量的生命周期'aIter的生命周期'c被声明为独立的。编译器不会尝试推断未明确指定的生命周期边界。该错误告诉您编译器无法证明'c不会超过'a,因为它们被声明为独立。

要修复它,您需要告诉编译器Iter不会超过它引用的向量。使用生命周期绑定'a: 'c就可以了。

fn init_iters<'a: 'c, 'b, 'c>( ...

Dragon Book