2016-06-14 37 views

cevap

5

Sana standart kütüphane kanalları ile yapardım bilmiyorum, ama chan crate bir chan_select! makro sağlamaktadır: Receiver::recv_timeout tanıtıldı

#[macro_use] 
extern crate chan; 

use std::time::Duration; 

fn main() { 
    let (_never_sends, never_receives) = chan::sync::<bool>(1); 
    let timeout = chan::after(Duration::from_millis(50)); 

    chan_select! { 
     timeout.recv() => { 
      println!("timed out!"); 
     }, 
     never_receives.recv() => { 
      println!("Shouldn't have a value!"); 
     }, 
    } 
} 
2

Pas 1.12:

use std::sync::mpsc::channel; 
use std::time::Duration; 

fn main() { 
    let (.., rx) = channel::<bool>(); 
    let timeout = Duration::new(3, 0); 

    println!("start recv"); 
    let _ = rx.recv_timeout(timeout); 
    println!("done!"); 
}