Rust語言之HelloWorld Web版
下面這篇文章值得仔細研讀:
http://arthurtw.github.io/2014/12/21/rust-anti-sloppy-programming-language.html
Iron是一個Web架構,是建立在hyper之上的,hyper是完全用Rust寫的http庫。因此,Iron相當於Tomcat/Jetty之於Java,
Cowboy之於Erlang。下面就使用Iron寫一個WebServer,很簡單,當使用者在瀏覽器地址欄訪問http://localhost:3000時,
瀏覽器返回: HelloWorld。
很簡單,就3步:
1) 安裝Rust。參考我的系列文章:http://blog.csdn.net/ubuntu64fan/article/details/47863935
2) 建立一個HelloWorld工程。參考我的系列文章:http://blog.csdn.net/ubuntu64fan/article/details/48370617
3) 修改hello_world/Cargo.toml, 增加下面的內容:
[dependencies]iron = "*"
修改hello_world/src/main.rs,全部內容如下:
extern crate iron;use iron::prelude::*;use iron::status;fn main() { fn hello_world(_: &mut Request) -> IronResult<Response> { Ok(Response::with((status::Ok, "Hello World!"))) } Iron::new(hello_world).http("localhost:3000").unwrap(); println!("On 3000");}
然後在hello_world/下面運行編譯命令:
# cargo build# cargo run
Running `target/debug/hello_world`
開啟瀏覽器,查看: localhost:3000
Hello World!
Rust 幾個術語:
cargo: rust語言的構建工具,很形象,貨車,把東西裝進去的意思。
crate: 相當於java的jar包,c/c++的so。柳條框,貨車裡裝上各種柳條框。
rustc: rust語言的編譯器。