rust - 创建可变迭代器时出现生命周期错误

标签 rust lifetime

尝试创建自定义迭代器时遇到了一些问题。不可使用的版本效果很好,但是在复制相同功能以创建可变版本时,会出现终身错误。这是我问题的简化版本:

struct Test {
    map: HashMap<u32, String>
}

impl Test {
    pub fn iter(&self, start: u32, end: u32) -> impl Iterator<Item = &String> {
        (start..=end).filter_map(move |i| {
            self.map.get(&i)
        })
    }

    pub fn iter_mut(&mut self, start: u32, end: u32) -> impl Iterator<Item = &mut String> {
        (start..=end).filter_map(move |i| {
            self.map.get_mut(&i)
        })
    }
}

iter 函数正常工作,但是 iter_mut 函数不会与此错误一起编译:

error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
  --> src/main.rs:21:22
   |
21 |             self.map.get_mut(&i)
   |                      ^^^^^^^
   |
note: first, the lifetime cannot outlive the lifetime `'_` as defined on the body at 20:34...
  --> src/main.rs:20:34
   |
20 |         (start..=end).filter_map(|i| {
   |                                  ^^^
note: ...so that closure can access `self`
  --> src/main.rs:21:13
   |
21 |             self.map.get_mut(&i)
   |             ^^^^^^^^
note: but, the lifetime must be valid for the anonymous lifetime defined on the method body at 19:21...
  --> src/main.rs:19:21
   |
19 |     pub fn iter_mut(&mut self, start: u32, end: u32) -> impl Iterator<Item = &mut String> {
   |                     ^^^^^^^^^
note: ...so that the types are compatible
  --> src/main.rs:19:57
   |
19 |     pub fn iter_mut(&mut self, start: u32, end: u32) -> impl Iterator<Item = &mut String> {
   |                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: expected `std::option::Option<&mut std::string::String>`
              found `std::option::Option<&mut std::string::String>`

最佳答案

正如 Todd 所说,由于创建了对同一 HashMap 的许多可变引用,可能会发生 iter_mut 上的编译错误,但我对此不确定。你可以这样做:

struct Test {
    map: HashMap<u32, String>
}

impl Test {
    pub fn iter(&self, start: u32, end: u32) -> impl Iterator<Item=&String> {
        self.map
            .iter()
            .filter_map(move |k| {
                if (start..=end).contains(k.0) {
                    Some(k.1)
                } else {
                    None
                }
            })
    }

    pub fn iter_mut(&mut self, start: u32, end: u32) -> impl Iterator<Item=&mut String> {
        self.map
            .iter_mut()
            .filter_map(move |k| {
                if (start..=end).contains(k.0) {
                    Some(k.1)
                } else {
                    None
                }
            })
    }
}

关于rust - 创建可变迭代器时出现生命周期错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68542666/

相关文章:

rust - 无法使用 std::process::Command - 没有这样的文件或目录

websocket - 我想在 HashMap 中保留一个引用,但我无法正确指定生命周期

rust - 为什么Rust不允许可变别名?

rust - 为什么我的 RefCell 零成本替代方案不是实现内部可变性的标准方法?

rust - 如何临时从结构中借用引用?

string - 如何在 Rust 中将字符串的字符串转换为字符串的数组/向量

templates - C++ for Rust 中特定模板用法的等价物

sqlite - 如何使用Diesel和SQLite获取新创建的值的ID?

rust - 为什么不能在同一结构中存储值和对该值的引用?

rust - 不同结构的实例作为函数的参数