This:
pub fn parse(&mut self) -> Result<Option<Rc<RefCell<Spec<C>>>>, ErrKind<C>> {
for n in self {
let spec = n.borrow();
if spec.exit == true {
return Ok(Some(Rc::clone(&n)));
}
}
self.validate()?;
Ok(None)
}
.. causes this:
Summary
error[E0382]: borrow of moved value: `self`
--> src/parser.rs:146:5
|
137 | pub fn parse(&mut self) -> Result<Option<Rc<RefCell<Spec<C>>>>, ErrKind<C>> {
| --------- move occurs because `self` has type `&mut parser::Parser<'_, C>`, which does not implement the `Copy` trait
138 | for n in self {
| ----
| |
| value moved here
| help: consider borrowing to avoid moving into the for loop: `&self`
...
146 | self.validate()?;
| ^^^^ value borrowed here after move
error: aborting due to previous error
Following the tips from the compiler causes:
Summary
error[E0277]: `&&mut parser::Parser<'a, C>` is not an iterator
--> src/parser.rs:138:14
|
138 | for n in &self {
| -^^^^
| |
| `&&mut parser::Parser<'a, C>` is not an iterator
| help: consider removing the leading `&`-reference
|
= help: the trait `std::iter::Iterator` is not implemented for `&&mut parser::Parser<'a, C>`
= note: `std::iter::Iterator` is implemented for `&mut &mut parser::Parser<'a, C>`, but not for `&&mut parser::Parser<'a, C>`
= note: required by `std::iter::IntoIterator::into_iter`
The Parser implements:
impl<'a, C> Iterator for Parser<'a, C> {
type Item = Rc<RefCell<Spec<C>>>;
fn next(&mut self) -> Option<Self::Item> {
match self.next() {
Ok(res) => { return res; },
Err(err) => {
self.err = Some(err);
return None;
}
}
}
}
I'm a little confused -- what do I need to implement to allow the iterator to borrow self
, allowing self.validate()
to be called after the iterator?