I have trouble using a std::iter::Peekable
. Why does the following code does not compile?
// rustc 1.7.0-nightly (b4707ebca 2015-12-27)
use std::iter::*;
struct Foo<'a> {
chars: Peekable<Chars<'a>>,
}
impl<'a> Foo<'a> {
fn foo(&mut self) {
self.chars.next(); // Ok
self.chars.skip_while(|c| true); // error: cannot move out of borrowed content [E0507]
}
}
skip_while takes self by value. But
chars
can't be moved because it's still mutably borrowed by the&mut self
. You can use by_ref to make sure the value skip_while gets is a reference to a wrapper, instead.