Actix-Web:穩定、快速的Rust語言Web框架(安裝與應用)

安裝:

由於Actix-Web是Rust框架,因此您將需要先安裝和配置好Rust來開始使用。 如果還沒有,我們建議您使用rustup來管理Rust的安裝。 Rust官方指南https://doc.rust-lang.org/book/ch01-01-installation.html

目前,Actix-Web至少需要最低版本Rust 1.39,因此請確保您更新運行rustup update,以獲取最新的Rust版本。

Actix-Web:穩定、快速的Rust語言Web框架(安裝與應用)

通過Rust的Cargo加入相關Actix-Web版本依賴即可自動運行使用:

發行版本:

<code>[dependencies]
actix-web = "2.0"/<code>

開發版本:

<code>[dependencies]
actix-web = { git = "https://github.com/actix/actix-web" }/<code>

如果您想按常規git clone從開源網站上下載也是可以的,操作如下:

<code>git clone https://github.com/actix/examples
cd examples/basics
cargo run/<code>

寫Hello, world!測試

用cargo命令創建新項目並cd到其文件夾上:

<code>cargo new hello-world
cd hello-world/<code>

找到Cargo.toml文件並在[dependencies]之下添加以下依賴有如以下代碼:

<code>[dependencies]
actix-web = "2.0"
actix-rt = "1.0"/<code>

在main.rs文件上添加以下代碼已啟動Web服務器並設好http服務器地址:

<code>use actix_web::{web, App, HttpResponse, HttpServer, Responder};

async fn index() -> impl Responder {
HttpResponse::Ok().body("Hello world!")
}

async fn index2() -> impl Responder {
HttpResponse::Ok().body("這裡還是 Hello world!")
}

#[actix_rt::main]
async fn main() -> std::io::Result {
HttpServer::new(|| {
App::new()
.route("/", web::get().to(index))
.route("/again", web::get().to(index2))
})
.bind("127.0.0.1:8088")?
.run()
.await
}/<code>

現在運行:

<code>cargo run/<code>

並在瀏覽器上打開:

<code>http://127.0.0.1:8088/<code>

好了,您自己動手試試吧!


Actix-Web:穩定、快速的Rust語言Web框架(安裝與應用)


Actix-Web:穩定、快速的Rust語言Web框架(安裝與應用)


分享到:


相關文章: