[英]Cannot move out of borrowed content [E0507]
I'm writing a lexer in Rust to learn, but I'm stuck with two "cannot move out of borrowed content [E0507]" errors. 我正在用Rust编写一个词法分析器来学习,但是遇到两个“无法移出借用的内容[E0507]”错误的问题。
I tried all the solutions out there, but nothing seems to work: RefCell
, clone()
, by_ref()
, changing the &mut self
to self
or &self
or mut self
, or dereferencing. 我尝试了所有解决方案,但似乎
RefCell
: RefCell
, clone()
, by_ref()
,将&mut self
更改为self
或&self
或mut self
或取消引用。
Here is my code: 这是我的代码:
struct Snapshot {
Index: u32,
}
struct Tokenizable<'a, T: 'a>
where T: Iterator
{
Index: u32,
Items: &'a T,
Snapshots: Vec<Snapshot>,
}
impl<'a, T> Tokenizable<'a, T>
where T: Iterator
{
fn new(items: &'a T) -> Tokenizable<'a, T> {
Tokenizable {
Index: 0,
Items: items,
Snapshots: Vec::new(),
}
}
fn end(&mut self) -> bool {
match self.Items.peekable().peek() {
Some(c) => false,
None => true,
}
}
fn peek(&mut self) -> Option<&T::Item> {
match self.Items.peekable().peek() {
Some(c) => Some(c),
None => None,
}
}
}
fn main() {}
error: cannot move out of borrowed content [E0507]
match self.Items.peekable().peek() {
^~~~~~~~~~
help: see the detailed explanation for E0507
error: borrowed value does not live long enough
match self.Items.peekable().peek() {
^~~~~~~~~~~~~~~~~~~~~
note: reference must be valid for the anonymous lifetime #1 defined on the block at 32:43...
fn peek(&mut self) -> Option<&T::Item> {
match self.Items.peekable().peek() {
Some(c) => Some(c),
None => None,
}
}
note: ...but borrowed value is only valid for the block at 32:43
fn peek(&mut self) -> Option<&T::Item> {
match self.Items.peekable().peek() {
Some(c) => Some(c),
None => None,
}
}
error: cannot move out of borrowed content [E0507]
match self.Items.peekable().peek() {
^~~~~~~~~~
help: see the detailed explanation for E0507
As you can see in the docs , the peekable
function takes the iterator by value. 如您在docs中
peekable
, peekable
函数按值接受迭代器。 Therefore it will only work if you own the iterator. 因此,只有在拥有迭代器的情况下,它才起作用。 However, in your code,
Items
is a shared reference to the iterator. 但是,在您的代码中,
Items
是对迭代器的共享引用。
Solving this problem requires approaching it from a different angle. 解决这个问题需要从不同的角度来解决。 For instance, you could take the iterator by value in the constructor and adapt the struct to store the peekable iterator in the
Items
field. 例如,您可以在构造函数中按值使用迭代器,并调整结构以将可查看的迭代器存储在
Items
字段中。
Basically, what is to be learned from here is the fact that over complicating things and over engineering things almost always does more harm than good. 基本上,从这里可以学到的事实是,过度复杂化和过度工程化总是弊大于利。
use std::iter::Peekable;
struct Snapshot {
index: u32
}
struct Tokenizable<T> where T: Iterator {
index : u32,
items : Peekable<T>,
snapshots : Vec<Snapshot>,
}
impl<T> Tokenizable<T> where T: Iterator {
fn new (items: T) -> Tokenizable<T> {
Tokenizable {
index : 0,
items : items.peekable (),
snapshots : Vec::new (),
}
}
fn end (&mut self) -> bool {
match self.items.peek () {
Some (c) => false,
None => true
}
}
fn peek (&mut self) -> Option<&<T as Iterator>::Item> {
match self.items.peek () {
Some (c) => Some (c),
None => None
}
}
}
fn main () {
let mut data = "Hello".chars ();
let tokenizable = Tokenizable::new (data);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.