Questions tagged «borrow-checker»

2
为什么不能在同一结构中存储值和对该值的引用?
我有一个值,我想以自己的类型存储该值和对该值中内容的引用: struct Thing { count: u32, } struct Combined<'a>(Thing, &'a u32); fn make_combined<'a>() -> Combined<'a> { let thing = Thing { count: 42 }; Combined(thing, &thing.count) } 有时,我有一个值,并且想在同一结构中存储该值和对该值的引用: struct Combined<'a>(Thing, &'a Thing); fn make_combined<'a>() -> Combined<'a> { let thing = Thing::new(); Combined(thing, &thing) } 有时,我什至没有引用该值,并且得到了相同的错误: struct Combined<'a>(Parent, Child<'a>); fn make_combined<'a>() …

1
无法移出借用的内容/不能移出共享引用的后面
我不明白这个错误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 …

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.