A general overview of the status of this domain can be found at http://arewewebyet.com/. At the time of writing, a number of web frameworks that provide vital support for basic needs are available. To get an initial idea of coding in practice, we have supplied a "hello world" example snippet for each of them:
- iron is the oldest framework. It was built for high concurrency needs, so it scales very well. The codebase has a high degree of modularity. Here is how you say "hello-world" in iron:
extern crate iron; use iron::prelude::*; use iron::status; fn main() { Iron::new(|_: &mut Request| { Ok(Response::with((status::Ok, "Hello World!"))) }).http("localhost:3000").unwrap(); }
- Another useful web framework inspired by express.js is nickel (https://github.com/nickel-org/nickel.rs). Say "hello world" in nickel like this:
#[macro_use] extern crate nickel; use nickel::{Nickel, HttpRouter}; fn main() { let mut server = Nickel::new(); server.get("**", middleware!("Hello World")); server.listen("127.0.0.1:6767"); }
- conduit provides a common HTTP server interface.
There is not yet the same level of functionality as the popular dynamically-typed web application frameworks (such as Rails, Phoenix, and Django) provide, but a few Rust frameworks, still under heavy development, are already emerging:
- Rocket, which has very good starting info and documentation on its website, https://rocket.rs. Say "hello world" in Rocket like this:
#![feature(plugin)] #![plugin(rocket_codegen)] extern crate rocket; #[get("/")] fn index() -> &'static str { "Hello, world!" } fn main() { rocket::ignite().mount("/", routes![index]).launch(); }
- Gotham (for more info, see https://github.com/gotham-rs/). Say "hello world" in Gotham like this:
extern crate futures; extern crate hyper; extern crate gotham; extern crate mime; use hyper::server::Http; use hyper::{Request, Response, StatusCode}; use gotham::http::response::create_response; use gotham::state::State; use gotham::handler::NewHandlerService; pub fn say_hello(state: State, _req: Request) -> (State, Response) { let res = create_response( &state, StatusCode::Ok, Some(( String::from("Hello World!").into_bytes(), mime::TEXT_PLAIN, )), ); (state, res) } pub fn main() { let addr = "127.0.0.1:7878".parse().unwrap(); let server = Http::new() .bind(&addr, NewHandlerService::new(|| Ok(say_hello))) .unwrap(); println!( "Listening on http://{}", server.local_addr().unwrap() ); server.run().unwrap(); }
If you only need a light micro-web framework, rustful could be your choice.
The most advanced and stable crate for developing HTTP applications at this moment is hyper. It is fast and contains both an HTTP client and server to build complex web applications. It is used by the iron web framework. To get started with it, read this introductory article http://zsiciarz.github.io/24daysofrust/book/vol1/day5.html.
For a lower-level library, you could start with tiny_http. reqwest, curl, and tokio-curl which are popular HTTP client libraries. If you need a Representational State Transfer (REST) framework, go for rustless.
And of course, don't ignore the new servo browser that is emerging!