我正在编写一个简单的TCP聊天引擎来学习Rust.
use std::io::{TcpListener, TcpStream};
use std::io::{Acceptor, Listener};
enum StreamOrSlice {
Strm(TcpStream),
Slc(uint, [u8, ..1024])
}
fn main() {
let listener = TcpListener::bind("127.0.0.1", 5555);
// bind the listener to the specified address
let mut acceptor = listener.listen();
let (tx, rx) = channel();
spawn(proc() {
let mut streams: Vec<TcpStream> = Vec::new();
match rx.recv() {
Strm(mut stream) => {
streams.push(stream);
}
Slc(len, buf) => {
for stream in streams.iter() {
stream.write(buf.slice(0, len));
}
}
}
});
// accept connections and process them, spawning a new tasks for each one
for stream in acceptor.incoming() {
match stream {
Err(e) => { /* connection failed */ }
Ok(mut stream) => {
// connection succeeded
tx.send(Strm(stream.clone()));
let tx2 = tx.clone();
spawn(proc() {
let mut buf: [u8, ..1024] = [0, ..1024];
loop {
let len = stream.read(buf);
tx2.send(Slc(len.unwrap(), buf));
}
})
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码无法编译:
Compiling chat v0.1.0 (file:///home/chris/rust/chat)
src/chat.rs:20:13: 20:29 error: the type of this value must be known in this context
src/chat.rs:20 Strm(mut stream) => {
^~~~~~~~~~~~~~~~
error: aborting due to previous error
Could not compile `chat`.
Run Code Online (Sandbox Code Playgroud)
这是什么原因?
值的类型是已知的,它在enumas中声明TcpStream.
我该如何修复此代码?
问题是,当您尝试匹配时rx.recv(),编译器不知道此表达式的类型,因为您使用泛型声明
let (tx, rx) = channel();
Run Code Online (Sandbox Code Playgroud)
它还没有可能推断出通用类型.
另外,因为它必须检查您是否正确覆盖了模式,所以它不能使用模式本身来推断类型.因此,您需要明确声明它,如下所示:
let (tx, rx) = channel::<StreamOrSlice>();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8646 次 |
| 最近记录: |