Playpen변경할 수있는 참조를 사용하여 값을 이동할 수 있습니까?
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
을이 작동하지 않습니다 어떻게 해결하는 이유는 대답은 설명하지만, 솔직히 그것은 바로 다음 stdlib의를 사용하는 것이 좋습니다 매우 가능성이 높습니다 API가 약간 다르더라도 '건너 뛰기'합니다 (변경 메소드 대신 반복자 어댑터입니다). 나는 동의 반복자를 돌연변이 일부 실험 후 – delnan
@delnan! 오류가 있습니다 –