macro_rules! select {
{ $( $var:pat = $event:expr $(; $sub:pat = $dep:expr)* => $body:expr ),+ $(,)? } => { ... };
}
Expand description
Selects over a range of possible future events, processing exactly one.
Inspired by equivalent behaviours in other programming languages such as Go
and Kotlin, and ultimately the select
system call from POSIX.
Which event gets processed is a case of bounded non-determinism: the implementation makes no guarantee about which event gets processed if multiple become possible around the same time, only that it will process one of them if at least one can be processed.
Examples
fn foo(ctx: Context) {
let mut x = 0;
let mut l = Loop::new(Duration::from_secs(1));
loop {
println!("x = {}", x);
x += 1;
select! {
_ = l.next() => continue,
_ = ctx.done() => break,
}
}
}