Rust语言之HelloWorld Web版
Rust語言之HelloWorld Web版
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) 創(chuàng)建一個HelloWorld工程。參考我的系列文章:http://blog.csdn.net/ubuntu64fan/article/details/48370617
3) 修改hello_world/Cargo.toml, 增加下面的內(nèi)容:
?
[dependencies]
iron = "*"
修改hello_world/src/main.rs,全部內(nèi)容如下:
?
?
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 幾個術(shù)語:
cargo: rust語言的構(gòu)建工具,很形象,貨車,把東西裝進去的意思。
crate: 相當于java的jar包,c/c++的so。柳條框,貨車里裝上各種柳條框。
rustc: rust語言的編譯器。
總結(jié)
以上是生活随笔為你收集整理的Rust语言之HelloWorld Web版的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 火了,挡不住了:Facebook Mov
- 下一篇: rust 面向对象之Struct、imp