Is there is a way to use Rust's channels like Go? I couldn't find any.
For those who are not familiar with select statement in Go (from the documentation):
A "select" statement chooses which of a set of possible send or receive operations will proceed. It looks similar to a "switch" statement but with the cases all referring to communication operations. A case with a RecvStmt may assign the result of a RecvExpr to one or two variables, which may be declared using a short variable declaration. The RecvExpr must be a (possibly parenthesized) receive operation. There can be at most one default case and it may appear anywhere in the list of cases.
Execution of a "select" statement proceeds in several steps:
- For all the cases in the statement, the channel operands of receive operations and the channel and right-hand-side expressions of send statements are evaluated exactly once, in source order, upon entering the "select" statement. The result is a set of channels to receive from or send to, and the corresponding values to send. Any side effects in that evaluation will occur irrespective of which (if any) communication operation is selected to proceed. Expressions on the left-hand side of a RecvStmt with a short variable declaration or assignment are not yet evaluated.
- If one or more of the communications can proceed, a single one that can proceed is chosen via a uniform pseudo-random selection. Otherwise, if there is a default case, that case is chosen. If there is no default case, the "select" statement blocks until at least one of the communications can proceed.
- Unless the selected case is the default case, the respective communication operation is executed.
- If the selected case is a RecvStmt with a short variable declaration or an assignment, the left-hand side expressions are evaluated and the received value (or values) are assigned.
- The statement list of the selected case is executed.
Since communication on nil channels can never proceed, a select with only nil channels and no default case blocks forever.
For example, how can I write this in Rust?
func search(ctx context.Context, result chan IResult, q string) error {
// googleSearch and bingSearch will return IResult interface channel
google := googleSearch(q)
bing := bingSearch(q)
t := time.After(time.Second)
for {
select {
// at any point if caller cancel the operation we return
case <- ctx.Done():
return nil
case r, ok := <- google:
if !ok { // check if channel is closed
google = nil
if bing == nil { // we are done
return nil
}
continue
}
// sending the google search result to result channel. ( channel to channel )
result <- r
case r, ok := <- bing:
if !ok {
bing = nil
if google == nil {
return nil
}
continue
}
result <- r
case <- t:
// this function never lives for more then 1 second
return fmt.Errorf("timeout")
}
}
return nil
}
If you just want to receive with a timeout, the recv_timeout
method is what you are looking for.
There is also a try_recv
method, if you want to receive without blocking.
If you want to mix several channels, then @Shepmaster's answer describes the only way I know of in the standard library.
However Rust's standard library is much more lightweight than Go's, and in Rust it is common to use crates for this kind of thing. The crossbeam_channel
crate does have a select
that is stable and can be used to wait on different channels.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With