vagrant@ubuntu-14:~/tmp/test/rustprimer/mysite$ cargo build
Compiling mysite v0.1.0 (file:///home/vagrant/tmp/test/rustprimer/mysite)
src/main.rs:29:36: 29:52 error: no method named `read_to_string` found for type `iron::request::Body<'_, '_>` in the current scope
src/main.rs:29 let payload = request.body.read_to_string();
^~~~~~~~~~~~~~~~
src/main.rs:29:36: 29:52 help: items from traits can only be used if the trait is in scope; the following trait is implemented but not in scope, perhaps add a `use` for it:
src/main.rs:29:36: 29:52 help: candidate #1: use `std::io::Read`
error: aborting due to previous error
Could not compile `mysite`.
src/main.rs:29:36: 29:52 help: items from traits can only be used if the trait is in scope; the following trait is implemented but not in scope, perhaps add a `use` for it:
src/main.rs:29:36: 29:52 help: candidate #1: use `std::io::Read`
Compiling mysite v0.1.0 (file:///home/vagrant/tmp/test/rustprimer/mysite)
src/main.rs:30:36: 30:52 error: this function takes 1 parameter but 0 parameters were supplied [E0061]
src/main.rs:30 let payload = request.body.read_to_string();
^~~~~~~~~~~~~~~~
src/main.rs:30:36: 30:52 help: run `rustc --explain E0061` to see a detailed explanation
src/main.rs:31:46: 31:53 error: mismatched types:
expected `&str`,
found `core::result::Result<usize, std::io::error::Error>`
(expected &-ptr,
found enum `core::result::Result`) [E0308]
src/main.rs:31 let request: Greeting = json::decode(payload).unwrap();
^~~~~~~
src/main.rs:31:46: 31:53 help: run `rustc --explain E0308` to see a detailed explanation
src/main.rs:30:36: 30:52 error: cannot infer an appropriate lifetime for lifetime parameter `'b` due to conflicting requirements [E0495]
src/main.rs:30 let payload = request.body.read_to_string();
^~~~~~~~~~~~~~~~
src/main.rs:29:5: 35:6 help: consider using an explicit lifetime parameter as shown: fn set_greeting<'a>(request: &mut Request<'a, 'a>) -> IronResult<Response>
src/main.rs:29 fn set_greeting(request: &mut Request) -> IronResult<Response> {
src/main.rs:30 let payload = request.body.read_to_string();
src/main.rs:31 let request: Greeting = json::decode(payload).unwrap();
src/main.rs:32 let greeting = Greeting { msg: request.msg };
src/main.rs:33 let payload = json::encode(&greeting).unwrap();
src/main.rs:34 Ok(Response::with((status::Ok, payload)))
...
error: aborting due to 3 previous errors
Could not compile `mysite`.
se std::io;use std::io::prelude::*;use std::fs::File;letmut f =try!(File::open("foo.txt"));letmut buffer =String::new();try!(f.read_to_string(&mut buffer));
用法比较简单,我们修改一下刚刚的函数:
fn set_greeting(request: &mut Request) -> IronResult<Response> {
let mut payload = String::new();
request.body.read_to_string(&mut payload);
let request: Greeting = json::decode(&payload).unwrap();
let greeting = Greeting { msg: request.msg };
let payload = json::encode(&greeting).unwrap();
Ok(Response::with((status::Ok, payload)))
}