Rust强大的actor系统和最有趣的Web框架

安装

类型安全

忘记字符串类型的对象,从请求到响应,一切都有类型

功能丰富

Actix提供了许多现成的功能。 HTTP/2,日志记录等

可扩展的

轻松创建任何Actix应用程序都可以使用的自己的库。

快如闪电

Actix的速度非常快。不要相信我们的话--自己看看吧!

use actix_web::{web, App, HttpRequest, HttpServer, Responder};

async fn greet(req: HttpRequest) -> impl Responder {
    let name = req.match_info().get("name").unwrap_or("World");
    format!("Hello {}!", &name)
}

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

灵活的响应

actix中的处理函数可以返回实现Responder特征的各种对象。从API返回一致的响应变得轻而易举。

#[derive(Serialize)]
struct Measurement {
    temperature: f32,
}

async fn hello_world() -> impl Responder {
    "Hello World!"
}

async fn current_temperature() -> impl Responder {
    web::Json(Measurement { temperature: 42.3 })
}