我不明白错误cannot move out of borrowed content
。我收到了很多次,我总是解决它,但我一直不明白为什么。
例如:
for line in self.xslg_file.iter() {
self.buffer.clear();
for current_char in line.into_bytes().iter() {
self.buffer.push(*current_char as char);
}
println!("{}", line);
}
产生错误:
error[E0507]: cannot move out of borrowed content
--> src/main.rs:31:33
|
31 | for current_char in line.into_bytes().iter() {
| ^^^^ cannot move out of borrowed content
在较新版本的 Rust 中,错误是
error[E0507]: cannot move out of `*line` which is behind a shared reference
--> src/main.rs:31:33
|
31 | for current_char in line.into_bytes().iter() {
| ^^^^ move occurs because `*line` has type `std::string::String`, which does not implement the `Copy` trait
我通过克隆解决了它line
:
for current_char in line.clone().into_bytes().iter() {
即使在阅读了其他帖子后,我也不明白这个错误:
这种错误的根源是什么?