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

Lifetime error when creating mutable iterator

我在尝试创建自定义迭代器时遇到了一些问题。非可变版本运行良好,但在复制相同函数以创建可变版本时,出现了生命周期错误。这是我的问题的简化版本:

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 所说,iter_mut 上的编译错误很可能是由于对同一个 HashMap 创建了许多可变引用,但我对此不确定。你可以这样做:

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
                }
            })
    }
}