I have code with a mutable reference with the 'static lifetime and I am trying to use it in the loop which has async move:
fn baz(_s: &mut String) {
// do something
}
fn main() {
let bar: &'static mut String = Box::leak(Box::new("test".to_string()));
loop {
tokio::spawn(async move {
baz(bar);
});
// some conditions here to exit from the loop
}
}
The Rust compiler suggests reborrowing, but it is syntactically incorrect, the first time I've stumbled on that:
error[E0382]: use of moved value: `bar`
--> src/main.rs:8:33
|
6 | let bar: &'static mut String = Box::leak(Box::new("test".to_string()));
| --- move occurs because `bar` has type `&mut String`, which does not implement the `Copy` trait
7 | loop {
8 | tokio::spawn(async move {
| _________________________________^
9 | | baz(bar);
| | --- use occurs due to use in generator
10 | | });
| |_________^ value moved here, in previous iteration of loop
|
help: consider creating a fresh reborrow of `bar` here
|
8 | tokio::spawn(async move &mut *{
| ++++++
How I can use this mutable reference in such a loop? I'm fighting against borrowing in the loop and async move wanting to do a copy/clone.
&mutat a time, so this is never gonna work. You should use some kind of synchronization, probably aMutex