Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 无法为结构[E0495]中具有相同生存期的多个引用的生存期参数推断适当的生存期_Rust_Lifetime - Fatal编程技术网

Rust 无法为结构[E0495]中具有相同生存期的多个引用的生存期参数推断适当的生存期

Rust 无法为结构[E0495]中具有相同生存期的多个引用的生存期参数推断适当的生存期,rust,lifetime,Rust,Lifetime,我的代码中有一个关于终身推断的错误,我已经能够将代码简化为以下内容: 使用std::collections::HashMap; 将A::test()函数更改为 fn测试(&'a mut self){//添加生存期规范 设a=a::new(self.x); } 它应该会起作用 编译器说它无法推断A::new()的生存期,第一个注释提到了“匿名生存期”,这意味着编译器不知道A::new(self.x)中的self.x的生存期。所以我们只需要告诉编译器,self的生命周期是'a这是否回答了您的问题?

我的代码中有一个关于终身推断的错误,我已经能够将代码简化为以下内容:

使用std::collections::HashMap;
将
A::test()
函数更改为

fn测试(&'a mut self){//添加生存期规范
设a=a::new(self.x);
}
它应该会起作用


编译器说它无法推断
A::new()
的生存期,第一个注释提到了“匿名生存期”,这意味着编译器不知道
A::new(self.x)
中的
self.x
的生存期。所以我们只需要告诉编译器,
self
的生命周期是
'a

这是否回答了您的问题?
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
  --> src/main.rs:13:17
   |
13 |         let a = A::new(self.x);
   |                 ^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 12:5...
  --> src/main.rs:12:5
   |
12 | /     fn test(&mut self) {
13 | |         let a = A::new(self.x);
14 | |     }
   | |_____^
note: ...so that reference does not outlive borrowed content
  --> src/main.rs:13:24
   |
13 |         let a = A::new(self.x);
   |                        ^^^^^^
note: but, the lifetime must be valid for the lifetime `'a` as defined on the impl at 7:6...
  --> src/main.rs:7:6
   |
7  | impl<'a> A<'a> {
   |      ^^
note: ...so that the expression is assignable
  --> src/main.rs:13:24
   |
13 |         let a = A::new(self.x);
   |                        ^^^^^^
   = note: expected `&mut std::collections::HashMap<&str, i32>`
              found `&mut std::collections::HashMap<&'a str, i32>`