2016-12-11 2 views
0

私は、ハイパーフレームワークでリバースプロキシを書くことでRustを学びたいと思っています。私のcomplete project is on GitHub。仕事と以下のコンパイルエラーで失敗しないことRust with hyperでWebサーバーを起動するにはどうすればよいですか?

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(); 

:私はexplained in the documentationとしてリスナーを起動するにこだわっている

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()に私の呼び出しが正しくありませんか?ドキュメントに示されているように新しいサーバーを作成してはいけませんか?

+1

https://github.com/hyperium/hyper/blob/0.9.x/examples/server.rs – Stargateur

答えて

3

Rustのすべての式は関数内になければならないので、fn main()でサーバを起動する必要があります。その後、それは動作します!

関連する問題