最佳答案
我不明白这个错误 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() {
我不明白这个错误,即使我读了其他的文章,比如:
这种错误的根源是什么?