As a very simple example, I'm trying to write a webserver that simply replies
this page has been requested $N times
but I'm having a lot of trouble sharing the mutable state for this to happen. Here's my best attempt:
extern crate hyper;
use hyper::Server;
use hyper::server::Request;
use hyper::server::Response;
struct World {
count: i64,
}
impl World {
fn greet(&mut self, req: Request, res: Response) {
self.count += 1;
let str: String = format!("this page has been requested {} times", self.count);
res.send(str.as_bytes()).unwrap();
}
}
fn main() {
println!("Started..");
let mut w = World { count: 0 };
Server::http("127.0.0.1:3001").unwrap()
.handle(move |req: Request, res: Response| w.greet(req, res) ).unwrap();
}