Iron::new()::http() 拦截标准输入
Iron::new()::http() intercepts stdin
我正在尝试使用 Rust 和 Iron 实现教育客户端-服务器应用程序。我遇到了我无法理解的行为。这是代码:
fn main() {
Iron::new(hello_world).http("localhost:3000").unwrap();
let mut input = String::new();
io::stdin().read_line(&mut input)
.expect("Failed to read line");
println!("You entered: {}", &input)
}
fn hello_world(_: &mut Request) -> IronResult<Response> {
Ok(Response::with((status::Ok, "Hello World!")))
}
当我 运行 它并尝试从键盘输入内容时,行 You entered: Some text 没有出现。
但是在我更改了这一行之后:
Iron::new(hello_world).http("localhost:3000").unwrap();
有了这个:
let listener = Iron::new(hello_world).http("localhost:3000").unwrap();
我得到了字符串 你在我的控制台上输入了一些文本。所以它似乎有效。但是现在我对未使用的变量发出警告。这种行为令人困惑。
谁能解释为什么会这样?
在您的代码的第一个版本中,第一行将阻止等待传入连接。这是因为:
Iron::new(hello_world).http("localhost:3000").unwrap()
生成一个 Listening
类型的对象,它将开始在单独的线程中侦听 http 请求。
Listening
结构实现了 Drop
特性,即任何 Listening
类型的对象在超出范围时将 运行 一个 drop
函数.所说的 drop 函数将 加入监听线程,阻止程序的进一步执行 。
- 通过不将
Listening
对象分配给变量,它会立即超出范围。这意味着 drop
函数是 运行 在对象创建之后 。
代码中的替代解释
您程序的第一个版本:
fn main() {
Iron::new(hello_world).http("localhost:3000").unwrap();
// The listening thread is joined here, so the program blocks
// The instructions below will never be executed
let mut input = String::new();
io::stdin().read_line(&mut input)
.expect("Failed to read line");
println!("You entered: {}", &input)
}
引入一个变量的结果:
fn main() {
let listener = Iron::new(hello_world).http("localhost:3000").unwrap();
let mut input = String::new();
io::stdin().read_line(&mut input)
.expect("Failed to read line");
println!("You entered: {}", &input)
// The listening thread is joined here, so the program blocks
// As you can see, the program will not exit
}
我正在尝试使用 Rust 和 Iron 实现教育客户端-服务器应用程序。我遇到了我无法理解的行为。这是代码:
fn main() {
Iron::new(hello_world).http("localhost:3000").unwrap();
let mut input = String::new();
io::stdin().read_line(&mut input)
.expect("Failed to read line");
println!("You entered: {}", &input)
}
fn hello_world(_: &mut Request) -> IronResult<Response> {
Ok(Response::with((status::Ok, "Hello World!")))
}
当我 运行 它并尝试从键盘输入内容时,行 You entered: Some text 没有出现。
但是在我更改了这一行之后:
Iron::new(hello_world).http("localhost:3000").unwrap();
有了这个:
let listener = Iron::new(hello_world).http("localhost:3000").unwrap();
我得到了字符串 你在我的控制台上输入了一些文本。所以它似乎有效。但是现在我对未使用的变量发出警告。这种行为令人困惑。
谁能解释为什么会这样?
在您的代码的第一个版本中,第一行将阻止等待传入连接。这是因为:
Iron::new(hello_world).http("localhost:3000").unwrap()
生成一个Listening
类型的对象,它将开始在单独的线程中侦听 http 请求。Listening
结构实现了Drop
特性,即任何Listening
类型的对象在超出范围时将 运行 一个drop
函数.所说的 drop 函数将 加入监听线程,阻止程序的进一步执行 。- 通过不将
Listening
对象分配给变量,它会立即超出范围。这意味着drop
函数是 运行 在对象创建之后 。
代码中的替代解释
您程序的第一个版本:
fn main() {
Iron::new(hello_world).http("localhost:3000").unwrap();
// The listening thread is joined here, so the program blocks
// The instructions below will never be executed
let mut input = String::new();
io::stdin().read_line(&mut input)
.expect("Failed to read line");
println!("You entered: {}", &input)
}
引入一个变量的结果:
fn main() {
let listener = Iron::new(hello_world).http("localhost:3000").unwrap();
let mut input = String::new();
io::stdin().read_line(&mut input)
.expect("Failed to read line");
println!("You entered: {}", &input)
// The listening thread is joined here, so the program blocks
// As you can see, the program will not exit
}