教程 rust tutorial

Web 后端服务

Rust 提供高性能且类型安全的 Web 框架,适用于构建稳定、可扩展的后端服务。流行的框架包括:

  • Actix Web:基于 actor 模型的高性能框架
  • Axum:使用 Tokio 和 Tower 构建,符合现代异步编程范式
  • Rocket:语法简洁,开发快速

本节以 Actix Web 为例演示一个简单的 HTTP 服务。

代码演示

Rust Playground
打开运行
use actix_web::{get, App, HttpServer, Responder, HttpResponse};

#[get("/")] async fn index() -> impl Responder { HttpResponse::Ok().body("Hello from Rust Web!") }

#[actix_web::main] async fn main() -> std::io::Result<()> { HttpServer::new(|| App::new().service(index)) .bind(("127.0.0.1", 8080))? .run() .await }