在win11进行Rust Web 开发,采用Salvo框架

介绍

Salvo,功能强大且简单易用的 Rust Web 框架

特性

  • 简单强大 - 零样板代码,会写函数就会写 Handler
  • HTTP/1、HTTP/2、HTTP/3 - 开箱即用的全协议支持
  • 灵活路由 - 树形路由,中间件可挂载到任意层级
  • 自动证书 - 内置 ACME,自动获取和续期 TLS 证书
  • OpenAPI - 一流的 OpenAPI 支持,自动生成文档
  • WebSocket & WebTransport - 内置实时通信支持
  • 基于 Hyper & Tokio - 生产级异步运行时

快速开始

bash 复制代码
cargo new hello-salvo
cd hello-salvo
cargo add salvo tokio --features salvo/oapi,tokio/macros

编写 src/main.rs:

rust 复制代码
use salvo::prelude::*;

#[handler]
async fn hello() -> &'static str {
    "Hello World"
}

#[tokio::main]
async fn main() {
    println!("=================================");
    println!("  Rust Server Starting...");
    println!("=================================");
    println!("  Address: 127.0.0.1:7878");
    println!("  Framework: Salvo");
    println!("  Status: Initializing...");
    println!("=================================\n");

    let router = Router::new().get(hello);
    let acceptor = TcpListener::new("127.0.0.1:7878").bind().await;

    println!("Server is ready and listening on http://127.0.0.1:7878");
    println!("Press Ctrl+C to stop the server\n");

    Server::new(acceptor).serve(router).await;
}

运行:

复制代码
cargo run

cargo加速

找到CARGO_HOME(在系统环境变量里配置的)

或者 %USERPROFILE%\.cargo

  1. 打开 Cargo 的目录

  2. 在该目录下,检查是否存在 config.toml文件。如果不存在,新建一个文本文档 ,并将其重命名为 config.toml(注意确保文件扩展名是 .toml,而不是 .txt)。

  3. 用记事本或其他文本编辑器打开 config.toml文件。

中科大源

中科大源

bash 复制代码
[source.crates-io]
replace-with = 'ustc'

[source.ustc]
registry = "sparse+https://mirrors.ustc.edu.cn/crates.io-index/"

[registries.ustc]
index = "sparse+https://mirrors.ustc.edu.cn/crates.io-index/"
相关推荐
1-1=018 分钟前
C# 委托与事件
开发语言·c#
Fuyo_111931 分钟前
C++ 运行时多态与虚函数机制全面剖析【面试重点】
开发语言·c++·面试
孟健1 小时前
Claude Code 同需求写两版对比:装不装 frontend-design 差多远?
前端·ai编程·claude
w_zero_one1 小时前
递归算法(一)
java·开发语言·算法
Julien20042 小时前
管理机密(一)
运维·服务器·开发语言·学习方法
Chen_harmony2 小时前
七、STL与string类
开发语言·c++
沐晓时光2 小时前
C语言入门,字符串和字符串函数知识讲解
c语言·开发语言
xxxiugou1233 小时前
C++命名空间与引用全解析
开发语言·c++
梦de天空一米16883 小时前
【一个重复id让游戏卡在62%,聊聊配置表检查】
前端
ryn483983 小时前
01 一个功能模块,怎么分层才不乱
前端·ai编程·claude