Rust語言之HelloWorld Web版_rust

來源:互聯網
上載者:User
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語言的編譯器。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.