Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 在动态创建的通道上选择_Rust - Fatal编程技术网

Rust 在动态创建的通道上选择

Rust 在动态创建的通道上选择,rust,Rust,我有: 我得到的错误是: use std::collections::{DList, Deque, TreeMap}; use std::comm::{Select, Handle}; fn main() { let mut list = DList::new(); let mut handles = TreeMap::new(); let select = Select::new(); for i in range(0, 3i) { //

我有:

我得到的错误是:

use std::collections::{DList, Deque, TreeMap};
use std::comm::{Select, Handle};

fn main() {
    let mut list = DList::new();
    let mut handles = TreeMap::new();

    let select = Select::new();

    for i in range(0, 3i) {

        // Create channel
        let (tx, rx) = channel();

        // Move receiver inside dlist
        list.push_front(rx);

        // Get the mut ref from the dlist
        let mut nrx = list.front_mut().unwrap();

        let handle = select.handle(nrx);
        let id = handle.id();
        handles.insert(id, handle);

        // Get the mut ref from the map and add to select
        unsafe { handles.find_mut(&id).unwrap().add(); }

        spawn(proc() {
            // Work with the channel
            let sender = tx;
        });

    }

    loop {
        let selected = select.wait();
        println!("Selected channel id: {}", selected);
    }

}

将代码更改为在每个调用try_recv的程序中使用Vec和iter,因为开发人员告诉我select没有为此做好准备(还?),但想知道哪些是其他解决方案,或者开发人员如何处理它。

找到了这个库,它有多个通道实现,也可以(安全地)实现选择编译时未知的通道。从文档中:

<anon>:21:22: 21:40 error: cannot infer an appropriate lifetime for lifetime parameter 'a in function call due to conflicting requirements
<anon>:21         let handle = select.handle(nrx);
                               ^~~~~~~~~~~~~~~~~~
<anon>:21:22: 21:28 note: first, the lifetime must be contained by the expression at 21:21...
<anon>:21         let handle = select.handle(nrx);
                               ^~~~~~
<anon>:21:22: 21:28 note: ...so that automatically reference is valid at the time of borrow
<anon>:21         let handle = select.handle(nrx);
                               ^~~~~~
<anon>:21:36: 21:39 note: but, the lifetime must also be contained by the expression at 21:35...
<anon>:21         let handle = select.handle(nrx);
                                             ^~~
<anon>:21:36: 21:39 note: ...so that automatically reference is valid at the time of borrow
<anon>:21         let handle = select.handle(nrx);
                                             ^~~
error: aborting due to previous error
playpen: application terminated with error code 101
use std::thread::{Thread};
use std::old_io::{timer};
use std::time::duration::{Duration};
use comm::{spsc};
use comm::select::{Select, Selectable};

let mut channels = vec!();
for i in 0..10 {
    let (send, recv) = spsc::one_space::new();
    channels.push(recv);
    Thread::spawn(move || {
        timer::sleep(Duration::milliseconds(100));
        send.send(i).ok();
    });
}
let select = Select::new();
for recv in &channels {
    select.add(recv);
}
let first_ready = select.wait(&mut [0])[0];
for recv in &channels {
    if first_ready == recv.id() {
        println!("First ready: {}", recv.recv_sync().unwrap());
        return;
    }
}