0

我想通过使用超框架编写反向代理来学习 Rust。我的完整项目在 GitHub 上如文档中所述,我坚持启动侦听器:

extern crate hyper;

use hyper::Client;
use hyper::server::{Server, Request, Response};
use std::io::Read;

fn pipe_through(req: Request, res: Response) {
    let client = Client::new();
    // Why does the response have to be mutable here? We never need to modify it, so we should be
    // able to remove "mut"?
    let mut response = client.get("http://drupal-8.localhost/").send().unwrap();
    // Print out all the headers first.
    for header in response.headers.iter() {
        println!("{}", header);
    }
    // Now the body. This is ugly, why do I have to create an intermediary string variable? I want
    // to push the response directly to stdout.
    let mut body = String::new();
    response.read_to_string(&mut body).unwrap();
    print!("{}", body);
}

Server::http("127.0.0.1:9090").unwrap().handle(pipe_through).unwrap();

这不起作用并且失败并出现以下编译错误:

error: expected one of `!` or `::`, found `(`
  --> src/main.rs:23:13
   |
23 | Server::http("127.0.0.1:9090").unwrap().handle(pipe_through).unwrap();
   |             ^

为什么我的电话http()不正确?它不应该按照文档中的说明创建一个新服务器吗?

4

1 回答 1

3

Rust 中的所有表达式都必须在一个函数中,所以我需要在 fn main() 中启动我的服务器。然后它起作用了!

于 2016-12-11T12:21:21.940 回答