小编Chr*_*son的帖子

在迭代器特征中指定关联类型的生命周期

我是 Rust 新手,我正在尝试找出在 Rust 中执行以下操作的最佳方法是什么:

struct ThingIterator {
    current: String,
    stop: String,
}

impl Iterator for ThingIterator {
    type Item = &str;
    fn next(&mut self) -> Option<&str> {
        if self.current == self.stop {
            return None;
        }
        // For testing
        self.current = self.stop;
        Some(&self.current)
    }
}

fn main() {
    let pi = ThingIterator {
        current: String::from("Ask"),
        stop: String::from("Zoo"),
    };
    println!("Number of things={}", pi.count());
}
Run Code Online (Sandbox Code Playgroud)

我的错误是:

error[E0106]: missing lifetime specifier
 --> src/main.rs:7:17
  |
7 |     type Item = &str;
  |                 ^ expected …
Run Code Online (Sandbox Code Playgroud)

rust

2
推荐指数
1
解决办法
495
查看次数

标签 统计

rust ×1