如何 return 使用 for 循环创建的 Vector?
How do I return a Vector created using a for loop?
我想使用 for 循环创建一些数据。我用下面的方法来做到这一点。
struct Message<'a> {
msg: &'a str
}
fn loop_function<'a>() -> Vec<Message<'a>> {
let mut result = vec![];
for x in 0..10 {
result.push(Message { msg: format!("{}", x).trim() });
}
result
}
fn main() {
loop_function();
}
但是当我尝试编译它时,我得到以下与 x
的生命周期相关的错误。
src/main.rs:8:33: 8:49 error: borrowed value does not live long enough
src/main.rs:8 result.push(Message { msg: format!("{}", x).trim() });
^~~~~~~~~~~~~~~~
src/main.rs:5:44: 11:2 note: reference must be valid for the lifetime 'a as defined on the block at 5:43...
src/main.rs:5 fn loop_function<'a>() -> Vec<Message<'a>> {
src/main.rs:6 let mut result = vec![];
src/main.rs:7 for x in 0..10 {
src/main.rs:8 result.push(Message { msg: format!("{}", x).trim() });
src/main.rs:9 }
src/main.rs:10 result
...
src/main.rs:8:6: 8:60 note: ...but borrowed value is only valid for the statement at 8:5
src/main.rs:8 result.push(Message { msg: format!("{}", x).trim() });
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/main.rs:8:6: 8:60 help: consider using a `let` binding to increase its lifetime
src/main.rs:8 result.push(Message { msg: format!("{}", x).trim() });
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
有没有办法延长生命周期 x
以便我可以 return 向量?
我。这是做不到的。 link 中的答案有很好的解释。我的解决方案是 return a String
:
struct Message {
msg: String
}
fn loop_function() -> Vec<Message> {
let mut result = vec![];
for x in 0..10 {
result.push(Message { msg: format!("{}", x).trim().to_string() });
}
result
}
fn main() {
loop_function();
}
我想使用 for 循环创建一些数据。我用下面的方法来做到这一点。
struct Message<'a> {
msg: &'a str
}
fn loop_function<'a>() -> Vec<Message<'a>> {
let mut result = vec![];
for x in 0..10 {
result.push(Message { msg: format!("{}", x).trim() });
}
result
}
fn main() {
loop_function();
}
但是当我尝试编译它时,我得到以下与 x
的生命周期相关的错误。
src/main.rs:8:33: 8:49 error: borrowed value does not live long enough
src/main.rs:8 result.push(Message { msg: format!("{}", x).trim() });
^~~~~~~~~~~~~~~~
src/main.rs:5:44: 11:2 note: reference must be valid for the lifetime 'a as defined on the block at 5:43...
src/main.rs:5 fn loop_function<'a>() -> Vec<Message<'a>> {
src/main.rs:6 let mut result = vec![];
src/main.rs:7 for x in 0..10 {
src/main.rs:8 result.push(Message { msg: format!("{}", x).trim() });
src/main.rs:9 }
src/main.rs:10 result
...
src/main.rs:8:6: 8:60 note: ...but borrowed value is only valid for the statement at 8:5
src/main.rs:8 result.push(Message { msg: format!("{}", x).trim() });
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/main.rs:8:6: 8:60 help: consider using a `let` binding to increase its lifetime
src/main.rs:8 result.push(Message { msg: format!("{}", x).trim() });
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
有没有办法延长生命周期 x
以便我可以 return 向量?
我String
:
struct Message {
msg: String
}
fn loop_function() -> Vec<Message> {
let mut result = vec![];
for x in 0..10 {
result.push(Message { msg: format!("{}", x).trim().to_string() });
}
result
}
fn main() {
loop_function();
}