首页 > 解决方案 > 创建可变迭代器时的生命周期错误

问题描述

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

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>`

标签: rustlifetime

解决方案


正如托德所说,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
                }
            })
    }
}


推荐阅读