'pass the pointer to the async function (rust)
I have already asked a question about this (link)
But I could not solve my problem because I want to pass the pointer like this (link)
use std::io;
use std::sync::Arc;
pub struct A {}
pub struct B<'a> {
pub i: &'a A,
}
impl<'a> B<'a> {
fn new(x: &'a A) -> Self {
Self { i: x }
}
async fn print_any(&self) {
println!("xxxxxxxxxxxxxxxxxxxxx")
}
}
#[tokio::main]
async fn main() -> io::Result<()> {
let aa = A {};
let bb = Arc::new(B::new(&aa));
for _ in 0..3 {
let b = Arc::clone(&bb);
tokio::spawn(async move {
b.print_any().await;
});
}
Ok(())
}
error[E0597]: `aa` does not live long enough
--> src/main.rs:24:30
|
24 | let bb = Arc::new(B::new(&aa));
| -------^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `aa` is borrowed for `'static`
...
34 | }
| - `aa` dropped here while still borrowed
A struct in the my real code is the database connection(sqlx) so i cant move that
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
