Answer#
Use one channel that carries a small enum. Everything the UI needs to know, progress and the final result or error, is a variant. The worker sends; the UI reads in its normal update step and stores the values in its state; the next frame draws the state.
use std::sync::mpsc;
use std::thread;
enum Msg {
Progress(u32), // percent
Done(Result<usize, String>),
}
fn main() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let items = 8;
for i in 1..=items {
// ... real work for one item here ...
let _ = tx.send(Msg::Progress(i * 100 / items));
}
let _ = tx.send(Msg::Done(Ok(items as usize)));
});
// In a real app this match sits in the per-frame or per-event update function.
for msg in rx {
match msg {
Msg::Progress(p) => println!("progress {p}%"),
Msg::Done(Ok(n)) => println!("done, {n} items"),
Msg::Done(Err(e)) => println!("failed: {e}"),
}
}
}progress 12%
progress 25%
progress 37%
progress 50%
progress 62%
progress 75%
progress 87%
progress 100%
done, 8 itemsInteger division makes the percentages round down (12, 25, 37 ...). In the real app, replace the println! calls with assignments such as self.percent = p; and draw a progress bar from self.percent.
Keep messages small and send them only when something changed visibly. A message per byte of a large file floods the channel; one per percent point or per 50 ms does not.
Common mistakes#
- Sending progress through a shared
Mutex<f32>that the UI polls without telling the toolkit to redraw. The channel plus a repaint request (egui) or a message (iced) is clearer. - Ending the work without a
Donemessage. The UI then cannot tell "finished" from "stuck". - Reporting errors only with
println!in the worker. Send them as a message so the window can show them.
Related questions#
Can the worker send results to several windows?#
One channel has one receiver. For several receivers, create one channel for each.
Sources#
- The Rust Book, "Using Message Passing to Transfer Data Between Threads": https://doc.rust-lang.org/book/ch16-02-message-passing.html (read 2026-12-05)