借用的值在异步函数中的寿命不够长
Borrowed value does not live long enough with async funtion
我是 Rust 的新手,正在尝试使一些代码与 运行 并行的一堆任务异步。这是一个简化的例子:
use futures::future::join_all;
#[tokio::main]
async fn main() {
let mut list = Vec::new();
for i in 1..10 {
let my_str = format!("Value is: {:?}", &i);
let future = do_something(&my_str);
list.push(future);
}
join_all(list).await;
}
async fn do_something(value: &str)
{
println!("value is: {:?}", value);
}
这在 do_something(&my_str) 调用中失败,并显示“借用的值寿命不够长”。我可以通过更改 do_something 以接受字符串而不是 &str 来编译代码。但是,当 &str 可以工作时要求 String 似乎有点奇怪。这里有更好的模式吗?谢谢!
“但是,当 &str
可以工作时要求字符串似乎有点奇怪。” 但是 &str
不能在这里工作,因为它只借用my_str
,它在未来完成之前被销毁:
for i in 1..10 {
// Create a new `String` and store it in `my_str`
let my_str = format!("Value is: {:?}", &i);
// Create a future that borrows `my_str`. Note that the future is not
// yet started
let future = do_something(&my_str);
// Store the future in `list`
list.push(future);
// Destroy `my_str` since it goes out of scope and wasn't moved.
}
// Run the futures from `list` until they complete. At this point each
// future will try to access the string that they have borrowed, but those
// strings have already been freed!
join_all(list).await;
相反,您的 do_something
应该取得字符串的所有权并负责释放它:
use futures::future::join_all;
#[tokio::main]
async fn main() {
let mut list = Vec::new();
for i in 1..10 {
// Create a new `String` and store it in `my_str`
let my_str = format!("Value is: {:?}", &i);
// Create a future and _move_ `my_str` into it.
let future = do_something(my_str);
// Store the future in `list`
list.push(future);
// `my_str` is not destroyed since it was moved into the future.
}
join_all(list).await;
}
async fn do_something(value: String)
{
println!("value is: {:?}", value);
// Destroy `value` since it goes out of scope and wasn't moved.
}
我是 Rust 的新手,正在尝试使一些代码与 运行 并行的一堆任务异步。这是一个简化的例子:
use futures::future::join_all;
#[tokio::main]
async fn main() {
let mut list = Vec::new();
for i in 1..10 {
let my_str = format!("Value is: {:?}", &i);
let future = do_something(&my_str);
list.push(future);
}
join_all(list).await;
}
async fn do_something(value: &str)
{
println!("value is: {:?}", value);
}
这在 do_something(&my_str) 调用中失败,并显示“借用的值寿命不够长”。我可以通过更改 do_something 以接受字符串而不是 &str 来编译代码。但是,当 &str 可以工作时要求 String 似乎有点奇怪。这里有更好的模式吗?谢谢!
“但是,当 &str
可以工作时要求字符串似乎有点奇怪。” 但是 &str
不能在这里工作,因为它只借用my_str
,它在未来完成之前被销毁:
for i in 1..10 {
// Create a new `String` and store it in `my_str`
let my_str = format!("Value is: {:?}", &i);
// Create a future that borrows `my_str`. Note that the future is not
// yet started
let future = do_something(&my_str);
// Store the future in `list`
list.push(future);
// Destroy `my_str` since it goes out of scope and wasn't moved.
}
// Run the futures from `list` until they complete. At this point each
// future will try to access the string that they have borrowed, but those
// strings have already been freed!
join_all(list).await;
相反,您的 do_something
应该取得字符串的所有权并负责释放它:
use futures::future::join_all;
#[tokio::main]
async fn main() {
let mut list = Vec::new();
for i in 1..10 {
// Create a new `String` and store it in `my_str`
let my_str = format!("Value is: {:?}", &i);
// Create a future and _move_ `my_str` into it.
let future = do_something(my_str);
// Store the future in `list`
list.push(future);
// `my_str` is not destroyed since it was moved into the future.
}
join_all(list).await;
}
async fn do_something(value: String)
{
println!("value is: {:?}", value);
// Destroy `value` since it goes out of scope and wasn't moved.
}