向量与结构的多次迭代 "cannot move out of borrowed content"
Multiple iterations of vector with structs "cannot move out of borrowed content"
我需要在循环中的每次迭代中用结构迭代一个向量。只要向量不包含结构,它就可以正常工作。我尝试了许多不同的解决方案,但总是遇到某种所有权问题。
我做错了什么?
struct Element {
title: String,
}
impl Element {
pub fn get_title(self) -> String {
self.title
}
}
fn main() {
let mut items: Vec<Element> = Vec::new();
items.push(Element {
title: "Random".to_string(),
});
items.push(Element {
title: "Gregor".to_string(),
});
let mut i = 0;
while i < 10 {
for item in &items {
println!("Loop {} item {}", i, item.get_title());
}
i = i + 1;
}
}
error[E0507]: cannot move out of borrowed content
--> src/main.rs:23:44
|
23 | println!("Loop {} item {}", i, item.get_title());
| ^^^^ cannot move out of borrowed content
问题是,您的 get_title
方法使用了 Element
,因此只能调用一次。
您必须接受 &self
作为参数,并且可以执行以下操作:
或者 return 一个 &str
而不是 String
:
pub fn get_title(&self) -> &str {
&self.title
}
或者克隆 String
如果你真的想要 return 一个 String
结构。
pub fn get_title(&self) -> String {
self.title.clone()
}
另请查看这些问题以进一步澄清:
- What are the differences between Rust's `String` and `str`?
- What types are valid for the `self` parameter of a method?
这里是问题的解决方案,它需要借用自身对象和生命周期规范.
从&String
移动到&str
只是为了遵循更好的做法,谢谢@hellow Playground 2
struct Element<'a> {
title: &'a str
}
impl <'a>Element<'a> {
pub fn get_title(&self) -> &'a str {
&self.title
}
}
fn main() {
let mut items: Vec<Element> = Vec::new();
items.push(Element { title: "Random" });
items.push(Element { title: "Gregor" });
let mut i = 0;
while i < 10 {
for item in &items {
println!("Loop {} item {}", i, item.get_title());
}
i = i + 1;
}
}
我需要在循环中的每次迭代中用结构迭代一个向量。只要向量不包含结构,它就可以正常工作。我尝试了许多不同的解决方案,但总是遇到某种所有权问题。
我做错了什么?
struct Element {
title: String,
}
impl Element {
pub fn get_title(self) -> String {
self.title
}
}
fn main() {
let mut items: Vec<Element> = Vec::new();
items.push(Element {
title: "Random".to_string(),
});
items.push(Element {
title: "Gregor".to_string(),
});
let mut i = 0;
while i < 10 {
for item in &items {
println!("Loop {} item {}", i, item.get_title());
}
i = i + 1;
}
}
error[E0507]: cannot move out of borrowed content
--> src/main.rs:23:44
|
23 | println!("Loop {} item {}", i, item.get_title());
| ^^^^ cannot move out of borrowed content
问题是,您的 get_title
方法使用了 Element
,因此只能调用一次。
您必须接受 &self
作为参数,并且可以执行以下操作:
或者 return 一个 &str
而不是 String
:
pub fn get_title(&self) -> &str {
&self.title
}
或者克隆 String
如果你真的想要 return 一个 String
结构。
pub fn get_title(&self) -> String {
self.title.clone()
}
另请查看这些问题以进一步澄清:
- What are the differences between Rust's `String` and `str`?
- What types are valid for the `self` parameter of a method?
这里是问题的解决方案,它需要借用自身对象和生命周期规范.
从&String
移动到&str
只是为了遵循更好的做法,谢谢@hellow Playground 2
struct Element<'a> {
title: &'a str
}
impl <'a>Element<'a> {
pub fn get_title(&self) -> &'a str {
&self.title
}
}
fn main() {
let mut items: Vec<Element> = Vec::new();
items.push(Element { title: "Random" });
items.push(Element { title: "Gregor" });
let mut i = 0;
while i < 10 {
for item in &items {
println!("Loop {} item {}", i, item.get_title());
}
i = i + 1;
}
}