当前位置: 首页 > 知识库问答 >
问题:

与self映射可变引用时存在冲突的生存期要求

尹兴生
2023-03-14

我试图编写一个方法,当给定HashMap的键数组时,它将返回一个Vec,其中包含对键对应值的可变引用。

struct Foo {
    bar: HashMap<u32, String>,
}

impl Foo {
    pub fn lookup(&mut self, keys: &[u32]) -> Vec<&mut String> {
        keys.iter()
            .filter(|&x| self.bar.contains_key(x))
            .map(|x| self.bar.get_mut(x).unwrap())
            .collect::<_>()
    }
}
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
  --> src/main.rs:11:31
   |
11 |             .map(|x| self.bar.get_mut(x).unwrap())
   |                               ^^^^^^^
   |
note: first, the lifetime cannot outlive the lifetime `'_` as defined on the body at 11:18...
  --> src/main.rs:11:18
   |
11 |             .map(|x| self.bar.get_mut(x).unwrap())
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...so that closure can access `self`
  --> src/main.rs:11:22
   |
11 |             .map(|x| self.bar.get_mut(x).unwrap())
   |                      ^^^^^^^^
note: but, the lifetime must be valid for the anonymous lifetime #1 defined on the method body at 8:5...
  --> src/main.rs:8:5
   |
8  | /     pub fn lookup(&mut self, keys: &[u32]) -> Vec<&mut String> {
9  | |         keys.iter()
10 | |             .filter(|&x| self.bar.contains_key(x))
11 | |             .map(|x| self.bar.get_mut(x).unwrap())
12 | |             .collect::<_>()
13 | |     }
   | |_____^
note: ...so that the expression is assignable
  --> src/main.rs:9:9
   |
9  | /         keys.iter()
10 | |             .filter(|&x| self.bar.contains_key(x))
11 | |             .map(|x| self.bar.get_mut(x).unwrap())
12 | |             .collect::<_>()
   | |___________________________^
   = note: expected  `std::vec::Vec<&mut std::string::String>`
              found  `std::vec::Vec<&mut std::string::String>`
pub fn lookup(&self, keys: &[u32]) -> Vec<&String> {
    keys.iter()
        .filter(|&x| self.bar.contains_key(x))
        .map(|x| self.bar.get(x).unwrap())
        .collect::<_>()
}

我假设这是一个“与借阅检查器作斗争”的问题,可能是由于self.bar被多次可变地借阅。我该怎么解决这个问题?

共有1个答案

康弘义
2023-03-14

下面的工作方法是使用提供的键过滤值,而不是将键映射到它们的值。正如SCappella正确指出的那样,如果提供了重复的有效密钥,问题中的代码将导致未定义的行为。

工作代码:

pub fn lookup(&mut self, keys: &[u32]) -> Vec<&mut String> {
   self.bar.iter_mut().filter_map(|(k, v)| {
         if keys.contains(k) {
             Some(v)
         } else {
             None
         }
   }).collect::<_>()
}
 类似资料:
  • 问题内容: 我不确定默认的python安装是否是我一直在安装模块的安装,是否可能是导致Unicode字节大小兼容性错误的原因。简而言之,我已经使用Python 2.7.3安装了Numpy 1.7,并且当我尝试安装另一个使用Python和Numpy作为依赖项的程序时,出现了以下错误: 所以我想我有一个有冲突的unicode字节大小(2字节vs. 4字节)。我去检查一下我是否有冲突的Python版本,

  • 我是hibernate新手,我遇到了以下问题。“唯一索引或主键冲突”。问题的出现是由于错误的映射,但我花了几个小时来找出为什么会发生这种情况。 我有一个超级类叫做数据结构 然后是关联两个元素的类关联。这里省略了类的某些部分,只是为了简化它。 这个类作为两个数据结构类型类之间的中间类。像这样。 TP-协会-TP TP等级: 或者激活类 总的来说,我增加了以下内容: 当我尝试添加相同的对象时,问题就出

  • 我正在尝试使用Spring启动应用程序将图像上传到azure blob。我得到了下面的错误 2022-02-02 23:28:39【qtp1371397528-21】信息16824 c.a.c.i.jackson。JacksonVersion-信息:包版本:jackson annotations=2.12.4,jackson core=2.12.4,jackson databind=2.12.4,

  • 生成路由映射缓存optimize:route 路由映射缓存用于开启路由延迟解析的情况下,支持路由反解的URL生成,如果你没有开启路由延迟解析或者没有使用URL路由反解生成则不需要生成。 生成路由映射缓存的命令: php think optimize:route 执行后,会在runtime目录下面生成route.php文件。

  • 我们将Spring Boot与其泽西Starter一起使用,并将其部署为WAR,以编程方式部署到另一个应用程序的嵌入式Tomcat中。 在我们的应用程序启动后,在某些环境中,会发生映射冲突并记录如下: 资源配置如下: 据我所知,Spring Boot泽西Starter将注册一个名为“jerseyServlet”的servlet映射到“/*”。 在某些环境中,泽西自己的将在启动后触发,由于现有的映射

  • 系统调用在调用进程的虚拟地址空间中提供映射,将文件或设备映射到内存中。 下面是两种类型 - 文件映射或文件支持的映射 - 此映射将进程的虚拟内存区域映射到文件。 这意味着读取或写入这些内存区域会导致文件被读取或写入。这是默认的映射类型。 匿名映射 - 此映射映射进程的虚拟内存区域,不受任何文件的支持。 内容被初始化为零。 这种映射类似于动态内存分配(malloc()),在某些实现中用于某些分配。

  • 这很可能通过Nexus配置得到解决。 我们使用maven进行hadoop开发。Nexus被配置为所有存储库的镜像,存储库被添加到Nexus公共组中。(参见Nexus中有没有更好的配置存储库的方法?) 我发现hadoop-core工件版本1.0.4显示来自spring-roo-repositoryhttp://spring-roo-repository.springsource.org/releas

  • This is the length of time in seconds that a template cache is valid. Once this time has expired, the cache will be regenerated. $caching must be set to "true" for $cache_lifetime to have any purpose.