在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/"
相关推荐
子兮曰3 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰3 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
小羊没烦恼!3 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
前端小万3 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
爱勇宝3 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
三十而立洋3 天前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
伞伞悦读3 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
卡布鲁3 天前
把一个 Vite + Vue3 应用塞进 qiankun (React + Umi3) 主站:十个坑的复盘
前端·javascript·react.js
C语言小火车3 天前
C/C++ 为什么需要编译器?
开发语言·c++
霍霍的袁3 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio