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个知识点)
变量与数据类型 (3个知识点)
所有权系统 (1个知识点)
借用与引用 (1个知识点)
生命周期 (1个知识点)
结构体 (2个知识点)
枚举与模式匹配 (3个知识点)
集合类型 (4个知识点)
闭包 (2个知识点)
迭代器 (2个知识点)
错误处理 (2个知识点)
包与模块 (2个知识点)
并发 (3个知识点)
异步编程 (3个知识点)
宏 (2个知识点)
Web 开发 (2个知识点)
FFI 与嵌入式 (2个知识点)
Cargo 与工具链 (2个知识点)
设计模式 (2个知识点)
进阶主题 (5个知识点)
使用说明
- • 本手册涵盖 Rust 从基础到并发、异步、Web 开发的完整知识体系
- • 在搜索框输入关键词,快速定位相关知识点
- • 点击条目标题进入详情页,查看详细说明与代码示例
- • 页面按分类展示所有知识点,方便系统性学习
- • 建议通过 Rust 官方文档(doc.rust-lang.org)加深理解