我有一个枚举类型的迭代器,其中一个变量包含可变引用。现在我想将这个引用从self
中移出并返回它。为了避免同时拥有两个指向同一对象的可变引用,我希望将self
的枚举变量更改为不包含该引用的变量。以下是代码示例:
enum Test<'a> {
A(&'a mut usize),
B,
}
impl<'a> Iterator for Test<'a> {
type Item = &'a mut usize;
fn next(&mut self) -> Option<Self::Item> {
match *self {
Test::A(r) => Some(r), // TODO: return mutable reference and change self to B
Test::B => None,
}
}
}
fn main() {
let mut v = 1;
let mut it = Test::A(&mut v);
it.next();
}
这个问题与更改可变引用下的匹配选择器有关,但那个解决方案在这里不起作用。