2

在 win8 上使用 Rust 0.8 运行以下测试程序(如下)时,如果我同时运行该程序的两个实例,当第一个程序等待输入(第 12 行)时,第二个程序中止(第 7 行)并出现致命错误下方留言。

为了防止致命错误,我假设我需要使用 match 语句,但是我对类型系统和使用 match 还不够了解,但我已经尝试过了。

在这种情况下,如何让下面的程序优雅地失败而不是中止?

示例测试程序:

001  use std::cell::Cell;
002  use std::rt::io::{Writer, Listener, Acceptor};
003  use std::rt::io::net::tcp::TcpListener;
004  use std::rt::io::net::ip::{SocketAddr, Ipv4Addr};
005
006  fn main() {
007      let res_tcp_acceptor = TcpListener::bind(SocketAddr { ip: Ipv4Addr(127, 0, 0, 1),
008                                                          port: 9876}).listen();
009   
010      print("Connected OK. Start Listening? (y/n)  : ");
011
012      let s_input: ~str = std::io::stdin().read_line();  
013
014      if s_input == ~"y" {

第二个程序实例显示的致命错误:

task <unnamed> failed at 'assertion failed: `(left == right) && (right == left)`
 (left: `0i32`, right: `-4092i32`)', C:\bot\slave\dist2-win\build\src\libstd\rt\
uv\net.rs:302
4

1 回答 1

1

That's a bug which should be reported.

The expected behaviour, as demonstrated by my Linux machine, is the raising of an IoError condition, which may be handled but which if unhandled will lead to task failure: task '<unnamed>' failed at 'Unhandled condition: io_error: rt::io::IoError{kind: OtherIoError, desc: "address already in use", detail: None}', /home/chris/vc/rust/src/libstd/condition.rs:131.

于 2013-11-01T07:05:57.577 回答