pub trait Skip<I: Iterator> {
fn skip(&mut self, steps: usize);
}
impl<I: Iterator> Skip<I> for I {
fn skip(&mut self, mut steps: usize) {
for _ in self {
steps -= 1;
if steps <= 0 {
break;
}
}
}
}
fn main() {
let s = "abc123def";
let mut chars = s.chars();
chars.skip(2);
println!("{:?}", chars.collect::<String>());
}
错误:
error: use of moved value: `chars` [--explain E0382]
--> <anon>:20:22
19 |> chars.skip(2);
|> ----- value moved here
20 |> println!("{:?}", chars.collect::<String>());
|> ^^^^^ value used here after move
<anon>:20:22: 20:27: note: in this expansion of format_args!
<anon>:20:22: 20:27: note: in this expansion of print! (defined in <std macros>)
<anon>:20:22: 20:27: note: in this expansion of println! (defined in <std macros>)
note: move occurs because `chars` has type `std::str::Chars<'_>`, which does not implement the `Copy` trait
error: aborting due to previous error
playpen: application terminated with error code 101
skip
。 - user395760