programming/rust

actix-web
Edited: Saturday 28 June 2025

Rust actix-web框架
目录
Github
官方文档
设置国内镜像
添加依赖
官方示例
下载依赖并编译运行
Github
https://github.com/actix/actix…
官方文档
https://actix.rs/docs/getting-…
设置国内镜像
cd ~/.cargo

创建config文件

vim config

添加如下镜像源

[source.crates-io]
replace-with = ‘rsproxy-sparse’
[source.rsproxy]
registry = “https://rsproxy.cn/crates.io-i…
[source.rsproxy-sparse]
registry = “sparse+https://rsproxy.cn/index/”
[registries.rsproxy]
index = “https://rsproxy.cn/crates.io-i…
[net]
git-fetch-with-cli = true
添加依赖
[dependencies]
actix-web = “4”
官方示例
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};

#[get(”/”)]
async fn hello() -> impl Responder {
HttpResponse::Ok().body(“Hello world!”)
}

#[post("/echo")]
async fn echo(req_body: String) -> impl Responder {
HttpResponse::Ok().body(req_body)
}

async fn manual_hello() -> impl Responder {
HttpResponse::Ok().body(“Hey there!”)
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(hello)
.service(echo)
.route("/hey", web::get().to(manual_hello))
})
.bind((“127.0.0.1”, 8080))?
.run()
.await
}
下载依赖并编译运行
cargo run
http://127.0.0.1:8080
http://127.0.0.1:8080/hey