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) 创建一个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语言的编译器。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

车斗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值