Macro std::select [] [src]

macro_rules! select {
    (
        $($name:pat = $rx:ident.$meth:ident() => $code:expr),+
    ) => { ... };
}
🔬 This is a nightly-only experimental API. (mpsc_select #27800)

A macro to select an event from a number of receivers.

This macro is used to wait for the first event to occur on a number of receivers. It places no restrictions on the types of receivers given to this macro, this can be viewed as a heterogeneous select.

Examples

#![feature(mpsc_select)]

use std::thread;
use std::sync::mpsc;

// two placeholder functions for now
fn long_running_thread() {}
fn calculate_the_answer() -> u32 { 42 }

let (tx1, rx1) = mpsc::channel();
let (tx2, rx2) = mpsc::channel();

thread::spawn(move|| { long_running_thread(); tx1.send(()).unwrap(); });
thread::spawn(move|| { tx2.send(calculate_the_answer()).unwrap(); });

select! {
    _ = rx1.recv() => println!("the long running thread finished first"),
    answer = rx2.recv() => {
        println!("the answer was: {}", answer.unwrap());
    }
}Run

For more information about select, see the std::sync::mpsc::Select structure.