使用 Iron 在简单的 Web 服务器中调用两次的函数

Function called twice in simple web server using Iron

我有一个超级简单的网络服务器,由于某种原因 println! 语句被打印了两次。为什么会这样?

extern crate iron;

use iron::prelude::*;
use iron::status;

fn hello_world(_: &mut Request) -> IronResult<Response> {
    println!("Said Hello World");
    Ok(Response::with((status::Ok, "Hello World\n")))
}

fn main() {
    Iron::new(hello_world).http("localhost:8000").unwrap();
}

每次我刷新浏览器时 shell 中的输出是:

Said Hello World
Said Hello World

我只希望打印出该行 once.It 似乎我的 hello_world 函数被调用了两次。我错过了什么?

您的浏览器可能会自动尝试获取 favicon.ico。检查Request的内容就知道了! :) 您的浏览器也可能有一个网络监视器作为其开发工具的一部分;这应该会向您显示 favicon.ico 的请求(我知道它在 Firefox 中有,不知道其他浏览器)。