std::select! [] [src]

macro_rules! select {
    (
        $($name:pat = $rx:ident.$meth:ident() => $code:expr),+
    ) => { ... };
}
Unstable (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)] fn main() { 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()); } } drop(rx1.recv()); drop(rx2.recv()); }
#![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());
    }
}

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