Rust 语言学习手册

免费学习 系统级编程 内存安全

Rust学习手册《Actix Web 框架基础》:Actix Web 是 Rust 中高性能的 Web 框架,基于 Actor 模型和 Tokio 异步运行时。 **依赖添加:** `Cargo.toml` ```toml [dependencies] actix-web = "4" `...

Web 开发

Actix Web 框架基础

【详细说明 & 代码示例】
Actix Web 是 Rust 中高性能的 Web 框架,基于 Actor 模型和 Tokio 异步运行时。 **依赖添加:** `Cargo.toml` ```toml [dependencies] actix-web = "4" ``` **基本使用:** ```rust use actix_web::{get, web, App, HttpResponse, HttpServer, Responder}; #[get("/")] async fn hello() -> impl Responder { HttpResponse::Ok().body("Hello, world!") } #[actix_web::main] async fn main() -> std::io::Result { HttpServer::new(|| { App::new() .service(hello) .route("/echo", web::post().to(echo)) }) .bind(("127.0.0.1", 8080))? .run() .await } async fn echo(req_body: String) -> impl Responder { HttpResponse::Ok().body(req_body) } ``` **路由与提取器:** - `web::Path` 提取路径参数 - `web::Query` 提取查询字符串 - `web::Json` 提取 JSON 请求体 ```rust async fn user_info(path: web::Path) -> impl Responder { let name = path.into_inner().0; HttpResponse::Ok().body(format!("User: {}", name)) } ``` **状态共享:** 使用 `web::Data` 共享应用状态。 **中间件:** 使用 `wrap` 添加日志、压缩、CORS 等。

学习提示:建议安装 Rust 工具链(rustup),使用 cargo 构建项目,并熟悉 rustc 编译器错误信息(Rust 的报错非常详细友好)。

全部章节目录(共 63 个知识点)

Rust 基础 (1个知识点)

所有权系统 (1个知识点)

借用与引用 (1个知识点)

生命周期 (1个知识点)

Cargo 与工具链 (2个知识点)

使用说明

  • • 本手册涵盖 Rust 从基础到并发、异步、Web 开发的完整知识体系
  • • 在搜索框输入关键词,快速定位相关知识点
  • • 点击条目标题进入详情页,查看详细说明与代码示例
  • • 页面按分类展示所有知识点,方便系统性学习
  • • 建议通过 Rust 官方文档(doc.rust-lang.org)加深理解