我正在尝试实现一个迭代器,该迭代器将逐行通过矩阵。
struct Matrix {
col_count: usize,
backing: Vec<bool>
}
fn get_row_mut<'a>(&'a mut self, row: usize) -> &'a mut [bool] {
&mut self.backing[row * self.col_count .. (row + 1) * self.col_count]
}
struct MatrixIterator<'a> {
matrix: &'a mut Matrix,
curr_row: usize
}
impl<'a> IntoIterator for &'a mut Matrix {
type Item = &'a mut [bool];
type IntoIter = MatrixIterator<'a>;
fn into_iter(self) -> Self::IntoIter {
MatrixIterator {matrix: self, curr_row: 0}
}
}
impl<'b, 'a: 'b> Iterator for MatrixIterator<'a> {
type Item = &'a mut [bool];
fn next(&mut self) -> Option<Self::Item> {
let row : &'b mut [bool] = self.matrix.get_row_mut(self.curr_row);
if self.curr_row < self.matrix.row_count {
Some(row)
} else {
None
}
}
}
出现错误:
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
--> src/matrix.rs:93:48
|
93 | let row : &'b mut [bool] = self.matrix.get_row_mut(self.curr_row);
| ^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 92:5...
--> src/matrix.rs:92:5
|
92 | fn next(&mut self) -> Option<Self::Item> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...so that reference does not outlive borrowed content
--> src/matrix.rs:93:36
|
93 | let row : &'b mut [bool] = self.matrix.get_row_mut(self.curr_row);
| ^^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime `'b` as defined on the impl at 90:6...
--> src/matrix.rs:90:6
|
90 | impl<'b, 'a: 'b> Iterator for MatrixIterator<'a> {
| ^^
note: ...so that reference does not outlive borrowed content
--> src/matrix.rs:93:36
|
93 | let row : &'b mut [bool] = self.matrix.get_row_mut(self.curr_row);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
首先,编译器是否正在尝试猜测get_row_mut返回的值的生存期?如果是这样,那么对于第一个冲突的要求,编译器关心的参考是哪个?另外,“一生难道不是一个完美的人选吗?”
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
--> src/matrix.rs:95:13
|
95 | Some(row)
| ^^^^^^^^^
|
note: first, the lifetime cannot outlive the lifetime `'b` as defined on the impl at 90:6...
--> src/matrix.rs:90:6
|
90 | impl<'b, 'a: 'b> Iterator for MatrixIterator<'a> {
| ^^
note: ...so that reference does not outlive borrowed content
--> src/matrix.rs:95:18
|
95 | Some(row)
| ^^^
note: but, the lifetime must be valid for the lifetime `'a` as defined on the impl at 90:10...
--> src/matrix.rs:90:10
|
90 | impl<'b, 'a: 'b> Iterator for MatrixIterator<'a> {
| ^^
note: ...so that the types are compatible
--> src/matrix.rs:92:46
|
92 | fn next(&mut self) -> Option<Self::Item> {
| ______________________________________________^
93 | | let row : &'b mut [bool] = self.matrix.get_row_mut(self.curr_row);
94 | | if self.curr_row < self.matrix.row_count {
95 | | Some(row)
... |
98 | | }
99 | | }
| |_____^
= note: expected `std::iter::Iterator`
found `std::iter::Iterator`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
对于此错误,我的处理过程为:Some(row)
的生存期不应长于b'-这意味着它可以是b'。因此,现在我们有一个要求,即b'在寿命a'内有效。好吧,如果a'== b',那也将是正确的。基本上Some(row)
的生存期应该为== a'== b'并且似乎都满足了这两个要求,但是我看不见的逻辑应该存在一个缺陷