rust - 什么是 Go 的 select case paradigm for channels in Rust?

标签 rust channel

有没有办法像 Go 一样使用 Rust 的 channel ?我找不到。

对于那些不熟悉 Go 中的 select 语句的人(来自 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:

  1. 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.
  2. 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.
  3. Unless the selected case is the default case, the respective communication operation is executed.
  4. 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.
  5. 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.

例如,我如何用 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
}

最佳答案

如果您只想超时接收, recv_timeout 方法就是您要找的。

还有一个 try_recv 方法,如果你想接收而不阻塞。

如果你想混合多个 channel ,那么@Shepmaster's answer描述了我在标准库中所知道的唯一方法。

然而,Rust 的标准库比 Go 的轻量级得多,在 Rust 中,通常使用 crate 来做这种事情。 crossbeam_channel crate 确实有一个 select 稳定,可用于在不同的 channel 上等待。

关于rust - 什么是 Go 的 select case paradigm for channels in Rust?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52302371/

相关文章:

go - 如果 sync.WaitGroup 类型的 Wait() 方法阻塞,因此不是异步的,为什么要使用它?

rust - 使用 actix-web 2.0 提供静态文件

java - Netty通过一个 channel 写入多条消息

go - 关闭已完成的 channel 后,goroutine中缺少打印内容

go - Golang 中的取消模式

Golang channel 让程序无限期等待

rust - 最少的内存使用和最佳性能

rust - 如何在 if-let 语句中将复制的变量引入为可变变量?

rust - 如何从函数返回 WeightedChoice?

rust - 如果尚未设置,如何设置嵌套在多个选项中的值?