想象一下一个小地图,其中存储了3个值,前两个是已知键。我想为此地图实现一个迭代器,但遇到了生命周期问题。从通用关联函数(下例中的K::zero()
)返回对值的引用的合适方法是什么?
仅供参考,我拥有特质,所以我尝试将其更改为新的RFC195关联的const
,但无济于事。
我将问题归结为以下代码:
extern crate num;
use num::*;
pub struct TinyMap<K: Num, V> {
v0: Option<V>, // value for K::zero()
v1: Option<V>, // value for K::one()
k2: K, // arbitrary K
v2: Option<V>, // value for k2
}
pub struct Iter<'a, K: 'a + Num, V: 'a> {
k0: K,
v0: &'a Option<V>,
v1: &'a Option<V>,
k2: &'a K,
v2: &'a Option<V>,
}
impl<K: Num, V> TinyMap<K, V> {
pub fn iter(&self) -> Iter<K, V> {
Iter {
k0: K::zero(),
v0: &self.v0,
v1: &self.v1,
k2: &self.k2,
v2: &self.v2,
}
}
}
impl<'a, K: 'a + Num, V: 'a> Iterator for Iter<'a, K, V> {
type Item = (&'a K, &'a V);
fn next(&mut self) -> Option<(&'a K, &'a V)> {
if (*self.v0).is_some() {
// code removed that remembers we did this once.
return Some((&self.k0, ((*self.v0).as_ref()).unwrap()));
}
// if (*self.v1).is_some() {
// code removed that remembers we did this once.
// return Some((&K::one(), &((*self.v1).unwrap())));
// }
None
}
}
error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements
--> src/lib.rs:38:26
|
38 | return Some((&self.k0, ((*self.v0).as_ref()).unwrap()));
| ^^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 35:5...
--> src/lib.rs:35:5
|
35 | / fn next(&mut self) -> Option<(&'a K, &'a V)> {
36 | | if (*self.v0).is_some() {
37 | | // code removed that remembers we did this once.
38 | | return Some((&self.k0, ((*self.v0).as_ref()).unwrap()));
... |
44 | | None
45 | | }
| |_____^
note: ...so that reference does not outlive borrowed content
--> src/lib.rs:38:26
|
38 | return Some((&self.k0, ((*self.v0).as_ref()).unwrap()));
| ^^^^^^^^
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 32:6...
--> src/lib.rs:32:6
|
32 | impl<'a, K: 'a + Num, V: 'a> Iterator for Iter<'a, K, V> {
| ^^
= note: ...so that the expression is assignable:
expected std::option::Option<(&'a K, &'a V)>
found std::option::Option<(&K, &V)>
答案 0 :(得分:0)
使用opencv
特征是不可能的,因为自引用的寿命(在代码中已经消失了,但是可以像这样显式地编写):
Iterator
由于type Item = (&'a K, &'a V);
fn next<'s>(&'s mut self) -> Self::Item;
不会出现在函数的返回值中(并且也不会出现在函数的返回值中,因为's
不能使用函数的类型参数),因此不允许输出保存对迭代器的任何成员变量的引用。
这是错误的机制,现在是为什么部分:
考虑一个函数,该函数确实包含对self成员的引用,并且所有生命周期均已正确设置:
Self::Item
与您尝试返回struct SomeMember;
struct SomeObject {
some_member: SomeMember,
}
impl SomeObject {
fn some_function<'s>(&'s mut self) -> &'s SomeMember {
&self.some_member
}
}
的方式相同,但是没有发生其他任何事情,并且固定了生存期,以便可以使用。但是,如果我尝试这样做:
&self.k
fn main() {
let mut some_object = SomeObject{some_member: SomeMember};
let _item_1 = some_object.some_function();
let _item_2 = some_object.some_function();
}
不允许第二次呼叫,因为它两次借用error[E0499]: cannot borrow `some_object` as mutable more than once at a time
--> src/main.rs:15:23
|
14 | let _item_1 = some_object.some_function();
| ----------- first mutable borrow occurs here
15 | let _item_2 = some_object.some_function();
| ^^^^^^^^^^^ second mutable borrow occurs here
16 | }
| - first borrow ends here
,可变地是经典的Rust no-no!但是,如果我尝试用借来了迭代器本身的Item类型实现迭代器,那么some_object
是不可能的,因为它试图一次拉出多个项!
因此,不,迭代器无法返回借用其内容的项目。这是迭代器特征合同的明确而有意的一部分。
答案 1 :(得分:0)
共识似乎是,从此时(Rust 1.29)开始,唯一明智的方法是将var animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]
console.log(animals.slice(1, 5));
// expected output: Array ["bison", "camel", "duck", "elephant"]
放在K::zero()
内。感谢@SvenMarnach确认我的怀疑。