尝试实现借用某些数据并拥有其他数据的迭代器时出现编译错误

时间:2017-11-01 12:00:25

标签: rust

我正在尝试实现迭代器:

struct MyIterator<'a> {
    s1: &'a str,
    s2: String,

    idx: usize,
}

impl<'a> MyIterator<'a> {
    fn new(s1: &str) -> MyIterator {
        MyIterator {
            s1: s1,
            s2: "Rust".to_string(),

            idx: 0,
        }
    }
}

impl<'a> Iterator for MyIterator<'a> {
    type Item = &'a str;

    fn next(&mut self) -> Option<Self::Item> {
        self.idx += 1;

        match self.idx {
            1 => Some(self.s1),
            2 => Some(&self.s2),
            _ => None,
        }
    }
}

我收到这个非常详细的错误消息,但我无法弄清楚如何修复代码:

error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements
  --> src\main.rs:39:23
   |
39 |             2 => Some(&self.s2),
   |                       ^^^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 34:5...
  --> src\main.rs:34:5
   |
34 | /     fn next(&mut self) -> Option<Self::Item> {
35 | |         self.idx + 1;
36 | |
37 | |         match self.idx {
...  |
41 | |         }
42 | |     }
   | |_____^
note: ...so that reference does not outlive borrowed content
  --> src\main.rs:39:23
   |
39 |             2 => Some(&self.s2),
   |                       ^^^^^^^^
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 31:1...
  --> src\main.rs:31:1
   |
31 | / impl<'a> Iterator for MyIterator<'a> {
32 | |     type Item = &'a str;
33 | |
34 | |     fn next(&mut self) -> Option<Self::Item> {
...  |
42 | |     }
43 | | }
   | |_^
note: ...so that types are compatible (expected std::iter::Iterator, found std::iter::Iterator)
  --> src\main.rs:34:46
   |
34 |       fn next(&mut self) -> Option<Self::Item> {
   |  ______________________________________________^
35 | |         self.idx + 1;
36 | |
37 | |         match self.idx {
...  |
41 | |         }
42 | |     }

为什么s2的生命周期不是'a

1 个答案:

答案 0 :(得分:1)

返回的值的类型为Option<&'a str>,但'a不会使MyIterator<'a>保持活动状态,因此它可能会超出范围,并且包含s2: String 。因此'a根本不会让s2保持活力。 (它只会使s1保持活着,如果您已经写过fn new(s1: &'a str) -> MyIterator<'a>,那么会更容易看到。

此外,Iterator特征的设计方式可以永远返回对Iterator函数中next本身存储内容的引用。< / p>

相反,您可以创建一个存储值的类型,并为它的引用实现IntoIterator(使用包含对存储对象的引用的单独迭代器类型)。