我正在寻找Response
使用以下代码设置 Iron 的标题:
extern crate iron; // 0.3.0
extern crate hyper; // 0.8.1
use iron::prelude::*;
use iron::status;
use hyper::header::{Headers, ContentType};
use hyper::mime::{Mime, TopLevel, SubLevel};
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
fn main() {
fn hello_world(_: &mut Request) -> IronResult<Response> {
let mut headers = Headers::new();
let string = getFileAsString("./public/index.html");
headers.set(
ContentType(Mime(TopLevel::Text, SubLevel::Html, vec![]))
);
Ok(Response::with((status::Ok, string, headers)))
}
Iron::new(hello_world).http("localhost:3000").unwrap();
println!("On 3000");
}
fn getFileAsString(fileStr: &str) -> String {
let path = Path::new(fileStr);
let display = path.display();
let mut fileContents = String::new();
let mut file = match File::open(&path) {
Err(why) => panic!("couldn't open {}: {}", display, Error::description(&why)),
Ok(file) => file,
};
match file.read_to_string(&mut fileContents) {
Err(why) => panic!("couldn't read {}: {}", display, Error::description(&why)),
Ok(_) => fileContents
}
}
但是我得到了错误:
error[E0277]: the trait bound `iron::Headers: iron::modifier::Modifier<iron::Response>` is not satisfied
--> src/main.rs:24:12
|
24 | Ok(Response::with((status::Ok, string, headers)))
| ^^^^^^^^^^^^^^ the trait `iron::modifier::Modifier<iron::Response>` is not implemented for `iron::Headers`
|
= note: required because of the requirements on the impl of `iron::modifier::Modifier<iron::Response>` for `(hyper::status::StatusCode, std::string::String, iron::Headers)`
= note: required by `iron::Response::with`
为什么我无法将标头传递到此元组中以供Request
构建器修改?