生锈的冲突寿命
conflicting lifetimes in rust
代码是让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<'_>`
...
所以我将 <'_> 更改为 <'a>,如下所示:
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`
...
不知道有没有人能帮我解决这个问题,谢谢!
首先:我强烈建议您重新考虑您的类型架构。为什么 Class
和 School
特质 ?您的不同 类 是否有不同的实现细节,例如,一个在内部使用 Vec
,另一个在内部使用 HashMap
?
正如 rodrigo 已经在评论中指出的那样,您只需删除一些 'a
s,它就会编译。原因是,用你的代码,你从字面上说,Coordinator::operate()
借用的可变 self 将与 Coordinator::main()
借用的一样长,但你在那里重新借用它。
然后看起来像这样:
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(&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(&mut self) {
self.operate();
if self.some_value == 0 {
println!("0");
}
}
fn operate(&mut self) {
self.score_handler.handle(self.school);
}
}
当然,你还必须定义一些实际的结构来实现这些特性,我举个例子:
struct ClassA {
student_scores: Vec<u64>,
}
impl Class for ClassA {
fn student_count(&self) -> usize {
self.student_scores.len()
}
fn student_score(&self, i: usize) -> u64 {
self.student_scores[i]
}
}
struct SchoolA<'a> {
classes: Vec<&'a dyn Class>,
}
impl<'a> School<'a> for SchoolA<'a> {
fn class_count(&self) -> usize {
self.classes.len()
}
fn class(&'a self, i: usize) -> &'a dyn Class {
self.classes[i]
}
}
struct ScoreHandlerA {
some_internal_state: u64,
}
impl<'a> ScoreHandler<'a> for ScoreHandlerA {
fn handle(&mut self, school: &'a dyn School<'a>) {
for i in 0..school.class_count() {
println!("Handling a class in a school");
}
}
}
然后,您的主要内容可能看起来像这样:
fn main() {
let classes = vec![ClassA {
student_scores: vec![13, 14, 15],
}];
let school = SchoolA {
classes: classes.iter().map(|c| c as &dyn Class).collect(),
};
let mut coordinator = Coordinator {
some_value: 13,
school: &school,
score_handler: &mut ScoreHandlerA {
some_internal_state: 0,
},
};
coordinator.main();
}
代码是让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<'_>`
...
所以我将 <'_> 更改为 <'a>,如下所示:
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`
...
不知道有没有人能帮我解决这个问题,谢谢!
首先:我强烈建议您重新考虑您的类型架构。为什么 Class
和 School
特质 ?您的不同 类 是否有不同的实现细节,例如,一个在内部使用 Vec
,另一个在内部使用 HashMap
?
正如 rodrigo 已经在评论中指出的那样,您只需删除一些 'a
s,它就会编译。原因是,用你的代码,你从字面上说,Coordinator::operate()
借用的可变 self 将与 Coordinator::main()
借用的一样长,但你在那里重新借用它。
然后看起来像这样:
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(&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(&mut self) {
self.operate();
if self.some_value == 0 {
println!("0");
}
}
fn operate(&mut self) {
self.score_handler.handle(self.school);
}
}
当然,你还必须定义一些实际的结构来实现这些特性,我举个例子:
struct ClassA {
student_scores: Vec<u64>,
}
impl Class for ClassA {
fn student_count(&self) -> usize {
self.student_scores.len()
}
fn student_score(&self, i: usize) -> u64 {
self.student_scores[i]
}
}
struct SchoolA<'a> {
classes: Vec<&'a dyn Class>,
}
impl<'a> School<'a> for SchoolA<'a> {
fn class_count(&self) -> usize {
self.classes.len()
}
fn class(&'a self, i: usize) -> &'a dyn Class {
self.classes[i]
}
}
struct ScoreHandlerA {
some_internal_state: u64,
}
impl<'a> ScoreHandler<'a> for ScoreHandlerA {
fn handle(&mut self, school: &'a dyn School<'a>) {
for i in 0..school.class_count() {
println!("Handling a class in a school");
}
}
}
然后,您的主要内容可能看起来像这样:
fn main() {
let classes = vec![ClassA {
student_scores: vec![13, 14, 15],
}];
let school = SchoolA {
classes: classes.iter().map(|c| c as &dyn Class).collect(),
};
let mut coordinator = Coordinator {
some_value: 13,
school: &school,
score_handler: &mut ScoreHandlerA {
some_internal_state: 0,
},
};
coordinator.main();
}