Rust语言之HelloWorld Web版
Rust語(yǔ)言之HelloWorld Web版
Rust語(yǔ)言之HelloWorld Web版
下面這篇文章值得仔細(xì)研讀:
http://arthurtw.github.io/2014/12/21/rust-anti-sloppy-programming-language.html
?
Iron是一個(gè)Web框架,是建立在hyper之上的,hyper是完全用Rust寫(xiě)的http庫(kù)。因此,Iron相當(dāng)于Tomcat/Jetty之于Java,
Cowboy之于Erlang。下面就使用Iron寫(xiě)一個(gè)WebServer,很簡(jiǎn)單,當(dāng)用戶在瀏覽器地址欄訪問(wèn)http://localhost:3000時(shí),
瀏覽器返回: HelloWorld。
很簡(jiǎn)單,就3步:
1) 安裝Rust。參考我的系列文章:http://blog.csdn.net/ubuntu64fan/article/details/47863935
2) 創(chuàng)建一個(gè)HelloWorld工程。參考我的系列文章:http://blog.csdn.net/ubuntu64fan/article/details/48370617
3) 修改hello_world/Cargo.toml, 增加下面的內(nèi)容:
?
[dependencies]
iron = "*"
修改hello_world/src/main.rs,全部?jī)?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/下面運(yùn)行編譯命令:
?
# cargo build
# cargo run
Running `target/debug/hello_world`
打開(kāi)瀏覽器,查看: localhost:3000
?
Hello World!?
Rust 幾個(gè)術(shù)語(yǔ):
cargo: rust語(yǔ)言的構(gòu)建工具,很形象,貨車(chē),把東西裝進(jìn)去的意思。
crate: 相當(dāng)于java的jar包,c/c++的so。柳條框,貨車(chē)?yán)镅b上各種柳條框。
rustc: rust語(yǔ)言的編譯器。
總結(jié)
以上是生活随笔為你收集整理的Rust语言之HelloWorld Web版的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 火了,挡不住了:Facebook Mov
- 下一篇: rust 面向对象之Struct、imp