【发布时间】:2020-08-11 07:46:00
【问题描述】:
代码是让score_handler在operate()方法中处理某个学校的分数,handle()只是做一些计算而不是保留self.school的引用:
trait Class{
fn student_count(&self, )->usize;
fn student_score(&self, i: usize) ->u64;
}
trait School<'a>{
fn class_count(&self)->usize;
fn class(&'a self, i:usize)->&'a dyn Class;
}
trait ScoreHandler<'a> {
fn handle(&'a mut self, school: &'a dyn School<'a>);
}
struct Coordinator<'a>{
some_value: u64,
school: &'a dyn School<'a>,
score_handler: &'a mut dyn ScoreHandler<'a>
}
impl Coordinator<'_>{
pub fn main(&mut self){
self.operate();
if self.some_value == 0 {
println!("0");
}
}
fn operate(&mut self){
self.score_handler.handle(self.school);
}
}
我遇到了错误:
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
-> mytest/main/src/main.rs:29:28
|
29 | self.score_handler.handle(self.school);
| ^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 28:5...
-> mytest/main/src/main.rs:28:5
|
28 | / fn operate(&mut self){
29 | | self.score_handler.handle(self.school);
30 | | }
| |_____^
note: ...so that reference does not outlive borrowed content
-> mytest/main/src/main.rs:29:9
|
29 | self.score_handler.handle(self.school);
| ^^^^^^^^^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime `'_` as defined on the impl at 21:18...
--> mytest/main/src/main.rs:21:18
|
21 | impl Coordinator<'_>{
| ^^
note: ...so that the types are compatible
--> mytest/main/src/main.rs:29:28
|
29 | self.score_handler.handle(self.school);
| ^^^^^^
= note: expected `&mut dyn ScoreHandler<'_>`
found `&mut dyn ScoreHandler<'_>`
...
所以我把改成了,像这样:
trait Class{
fn student_count(&self, )->usize;
fn student_score(&self, i: usize) ->u64;
}
trait School<'a>{
fn class_count(&self)->usize;
fn class(&'a self, i:usize)->&'a dyn Class;
}
trait ScoreHandler<'a> {
fn handle(&'a mut self, school: &'a dyn School<'a>);
}
struct Coordinator<'a>{
some_value: u64,
school: &'a dyn School<'a>,
score_handler: &'a mut dyn ScoreHandler<'a>
}
impl<'a> Coordinator<'a>{
pub fn main(&'a mut self){
self.operate();
if self.some_value == 0 {
println!("0");
}
}
fn operate(&'a mut self){
self.score_handler.handle(self.school);
}
}
我得到了错误:
error[E0503]: cannot use `self.some_value` because it was mutably borrowed
--> mytest/main/src/main.rs:24:12
|
21 | impl<'a> Coordinator<'a>{
| -- lifetime `'a` defined here
22 | pub fn main(&'a mut self){
23 | self.operate();
| --------------
| |
| borrow of `*self` occurs here
| argument requires that `*self` is borrowed for `'a`
24 | if self.some_value == 0 {
| ^^^^^^^^^^^^^^^ use of borrowed `*self`
error[E0503]: cannot use `self.some_value` because it was mutably borrowed
--> mytest/main/src/main.rs:24:12
|
21 | impl<'a> Coordinator<'a>{
| -- lifetime `'a` defined here
22 | pub fn main(&'a mut self){
23 | self.operate();
| --------------
| |
| borrow of `*self` occurs here
| argument requires that `*self` is borrowed for `'a`
24 | if self.some_value == 0 {
| ^^^^^^^^^^^^^^^ use of borrowed `*self`
...
我想知道是否有人可以帮助我解决这个问题,谢谢!
【问题讨论】:
-
如果你删除除了
Coordinator<'a>it compiles之外的所有。这对你有用吗? -
@rodrigo,它有效,谢谢!但是我会检查为什么我将生命周期用于学校等,因为这是一个简化的演示,与我的原始代码完全不同,我使用生命周期来解决另一个问题。很快回来。