A practical guide to implementing cancelable background tasks in Rust using crossbeam channels. Covers making a task cancelable by passing a receiver channel, building a Worker struct that queues tasks, handles cancellation signals, and gracefully shuts down on drop. Key details include cleaning stale cancel signals before starting a new task, skipping queued tasks when a die signal is present, and using the Drop trait for automatic cleanup. Also briefly touches on returning values from tasks and async/tokio alternatives.
Sort: