Return value
The tokio::select!
macro returns the result of the evaluated <handler>
expression.
async fn computation1() -> String {
// .. computation
}
async fn computation2() -> String {
// .. computation
}
#[tokio::main]
async fn main() {
let out = tokio::select! {
res1 = computation1() => res1,
res2 = computation2() => res2,
};
println!("Got = {}", out);
}
Because of this, it is required that the <handler>
expression for each branch evaluates to the same type. If the output of a select!
expression is not needed, it is good practice to have the expression evaluate to ()
.