在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/"
相关推荐
lhbian6 小时前
C++、C与易语言:编程语言对比解析
c语言·开发语言·c++
꧁细听勿语情꧂6 小时前
数据结构概念和算法、时间复杂度、空间复杂度引入
c语言·开发语言·数据结构·算法
说实话起个名字真难啊6 小时前
2026数字中国创新大赛数字安全赛道writeup之web题目一
java·前端·安全
jerrywus6 小时前
Claude Code 真正的用法:skill / agent / hooks / worktree 一篇全搞定
前端·agent·claude
陈健平6 小时前
AI漫剧工具复刻实战:用 React Flow 搭一个前端的无限画布,复刻 TapNow / LiblibTV 的核心交互
前端·人工智能·react.js
0xDevNull6 小时前
Java 深度解析:for 循环 vs Stream.forEach 及性能优化指南
java·开发语言·性能优化
研☆香6 小时前
聊一聊如何分析js中的数据结构
开发语言·javascript·数据结构
-凌凌漆-6 小时前
【Qt】 QSerialPort::flush()介绍
开发语言·qt
徐子元竟然被占了!!7 小时前
IS-IS协议
开发语言·网络·php