Skip to content

use select! oneshot "called after complete" error #3812

Answered by Darksonn
daiguadaidai asked this question in Q&A
Discussion options

You must be logged in to vote

The easiest is to use a channel that has support for using it after it has been closed such as an tokio::sync::mpsc. Incidentally, this is also the channel that is closest to a Go channel.

use tokio::sync::mpsc;

#[tokio::main]
async fn main() {
    let (tx1, mut rx1) = mpsc::channel(1);
    let (tx2, mut rx2) = mpsc::channel(1);

    tokio::spawn(async move {
        let _ = tx1.send("one").await;
    });

    tokio::spawn(async move {
        let _ = tx2.send("two").await;
    });

    for _ in 0..2 {
        tokio::select! {
            Some(val) = rx1.recv() => {
                println!("rx1 comopleted first with {:?}", val);
            }
            Some(val) = rx2.recv() => {

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@daiguadaidai
Comment options

Answer selected by Darksonn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants