本文介绍了Rust:错误[E0495]:由于需求冲突,无法为autoref推断适当的生存期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是最小的代码:
struct Node<T> {
item: T,
next: Link<T>,
}
type Link<T> = Option<Box<Node<T>>>;
pub struct IterMut<'a, T>(&'a mut Link<T>);
impl<'a, T> Iterator for IterMut<'a, T> {
type Item = &'a mut T;
fn next(&mut self) -> Option<Self::Item> {
self.0.as_mut().map(|boxed_node| {
self.0 = &mut boxed_node.next;
&mut boxed_node.item
})
}
}
据我了解,应该没有问题.我已经做了很多搜索,但是没有办法.
As far as I understand, there should be no problem. I have done a lot of searching, but no way.
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
--> src/lib.rs:13:16
|
13 | self.0.as_mut().map(|boxed_node| {
| ^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 12:5...
--> src/lib.rs:12:5
|
12 | / fn next(&mut self) -> Option<Self::Item> {
13 | | self.0.as_mut().map(|boxed_node| {
14 | | self.0 = &mut boxed_node.next;
15 | | &mut boxed_node.item
16 | | })
17 | | }
| |_____^
note: ...so that reference does not outlive borrowed content
--> src/lib.rs:13:9
|
13 | self.0.as_mut().map(|boxed_node| {
| ^^^^^^
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 10:6...
--> src/lib.rs:10:6
|
10 | impl<'a, T> Iterator for IterMut<'a, T> {
| ^^
note: ...so that reference does not outlive borrowed content
--> src/lib.rs:14:22
|
14 | self.0 = &mut boxed_node.next;
| ^^^^^^^^^^^^^^^^^^^^
推荐答案
我们可以将您的代码重写为:
We can rewrite your code as:
struct Node<T> {
item: T,
next: Link<T>,
}
type Link<T> = Option<Box<Node<T>>>;
pub struct IterMut<'a, T>(&'a mut Link<T>);
impl<'a, T> Iterator for IterMut<'a, T> {
type Item = &'a mut T;
fn next(&mut self) -> Option<Self::Item> {
if let Some(boxed_node) = self.0 {
self.0 = &mut boxed_node.next;
Some(&mut boxed_node.item)
}
else {
None
}
}
}
您可以看到boxed_node
生命在该函数的结尾处结束,因此您无法返回对其的引用链接.
You can see that boxed_node
life end at the end of the function so you can't return a reference link to it.
解决方案是引用该框而不是引用该选项:
The solution is to take a reference of the box and not a reference to the option:
struct Node<T> {
item: T,
next: Link<T>,
}
type Link<T> = Option<Box<Node<T>>>;
pub struct IterMut<'a, T>(Option<&'a mut Box<Node<T>>>);
impl<'a, T> Iterator for IterMut<'a, T> {
type Item = &'a mut T;
fn next(&mut self) -> Option<Self::Item> {
if let Some(boxed_node) = self.0.take() {
self.0 = boxed_node.next.as_mut();
Some(&mut boxed_node.item)
}
else {
None
}
}
}
您也可以删除Box
:
struct Node<T> {
item: T,
next: Link<T>,
}
type Link<T> = Option<Box<Node<T>>>;
pub struct IterMut<'a, T>(Option<&'a mut Node<T>>);
impl<'a, T> Iterator for IterMut<'a, T> {
type Item = &'a mut T;
fn next(&mut self) -> Option<Self::Item> {
if let Some(boxed_node) = self.0.take() {
self.0 = boxed_node.next.as_mut().map(AsMut::as_mut);
Some(&mut boxed_node.item)
}
else {
None
}
}
}
这篇关于Rust:错误[E0495]:由于需求冲突,无法为autoref推断适当的生存期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!