Getting an error from thread
⚓ Rust 📅 2026-04-08 👤 surdeus 👁️ 6Currently I use an approach as below
use std::{error::Error, thread};
fn main() {
let thread = thread::spawn(move || {
let try = || -> Result<(), Box<dyn Error>> {
let mut res = 0;
for _ in 0..7 {
res += 2;
if res > 4 {
return Err("too much".into());
}
}
Ok(())
};
if try().is_err() {
println!("err in thread processing")
}
});
thread.join();
println!("thread ended")
}
it works great, however if I want to analyze the error in a caller thread, there is no such function as:
println!("error in thread {}", thread.get_err());
AI recommend to use a channel to get a thread error in the caller thread, however I do not want to introduce a channel just for errors. Which approach do you use?
6 posts - 3 participants
🏷️ Rust_feed