I posted the same question in users forum, however, I could not find an acceptable answer (other than a workaround) for the question.
Please consider the following code:
use std::thread;
use std::sync::mpsc;
use std::sync::mpsc::Receiver;
fn handle(_rx:Receiver<(i32, &str)>) {
// ...
}
fn main() {
let s = String::from("Some text");
let (tx, rx) = mpsc::channel();
thread::spawn(move|| handle(rx));
loop {
// Endless loop
tx.send((5, s.as_str())).unwrap();
}
}
the code is not compiled with the following error:
error[E0597]: `s` does not live long enough
--> a.rs:17:21
|
17 | tx.send((5, s.as_str())).unwrap();
| ^ does not live long enough
18 | }
19 | }
| - borrowed value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
However, since s has been defined in the main function, it would live for the entire life of program (i.e. static lifetime).
rustc 1.23.0 (766bd11c8 2018-01-01)