Rust web 框架 axum (一): 安装及 hello world - rust - 大象笔记


本站和网页 https://www.sunzhongwei.com/rust-web-framework-axum-install-and-hello-world 的作者无关,不对其内容负责。快照谨为网络故障时之索引,不代表被搜索网站的即时页面。

Rust web 框架 axum (一): 安装及 hello world - rust - 大象笔记
首页
关于我
哼哼
读书笔记
Rust web 框架 axum (一): 安装及 hello world
更新日期: 2023-11-09 阅读次数: 1461
字数: 648
分类: rust
搜索
为何要尝试 axum
最近在看两本 Rust 的教程,一本在早上上厕所时看,一本在班车上看/睡觉前看。这两个作者确实经验丰富,文笔也非常有趣,比刷抖音愉快多了。是个很好的放松方式。
但是光看不练,学习效率就很低,所以决定一边看书,一边写点 Rust 代码巩固一下。
虽然我对于 Rust 还没有实现的项目用途,但可以写一点 web 小工具,毕竟每天实际工作中每天都会遇到一些繁琐的手动操作需要自动化。之前学习 golang 就是,练手写了一个微信小程序的后台,写完后感觉对 golang 就尽在掌握了。。。这个小程序至今已积累了 30 万用户,日活 2k,也非常有成就感。
axum 简介
axum is a web application framework that focuses on ergonomics (人体工程学) and modularity (模块化).
创建项目
如果没有安装 rust cargo,可以参考 使用国内源安装 Rust。
新建一个名为 rust_tool 的项目。
cargo new rust_tool
hello world 代码
用下面代码替换掉 src/main.rs 中的默认代码:
use axum::{
routing::get,
Router,
};
#[tokio::main]
async fn main() {
// build our application with a single route
let app = Router::new().route("/", get(|| async { "Hello, World!" }));
// run it with hyper on localhost:3000
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
运行报错
> cargo run
Compiling rust_tool v0.1.0 (/mnt/d/work/rust_tool)
error[E0433]: failed to resolve: use of undeclared crate or module `axum`
--> src/main.rs:1:5
1 | use axum::{
| ^^^^ use of undeclared crate or module `axum`
error[E0432]: unresolved import `axum`
--> src/main.rs:1:5
1 | use axum::{
| ^^^^ use of undeclared crate or module `axum`
看来是没有安装相关的依赖包。这点不如 go 的包管理器方便,至少能给出安装提示命令。
Cargo.toml 添加依赖
打开配置文件 Cargo.toml, 在 dependencies 下加入 axum 和 tokio 两个依赖。
[package]
name = "rust_tool"
version = "0.1.0"
edition = "2021"
[dependencies]
axum = "0.6"
tokio = { version = "1.0", features = ["full"] }
目前(2023-11-04 星期六), axum 的最新版本为 0.6
tokio 是一个异步的 async 库。
或者通过 cargo add 命令来添加依赖包:
cargo add axum \
tokio -F tokio/full
再次运行
cargo run
就能看到 cargo 在自动安装依赖了。
当看到
Compiling rust_tool v0.1.0 (/mnt/d/work/rust_tool)
Finished dev [unoptimized + debuginfo] target(s) in 3m 28s
Running `target/debug/rust_tool`
说明编译完成,并运行了。
此时,打开浏览器输入
http://localhost:3000
即可看到 Hello, World! 的字样。
依赖包下载慢
参考:
https://mirrors.tuna.tsinghua.edu.cn/help/rustup/
若要长期启用镜像源,执行:
# for bash
echo 'export RUSTUP_UPDATE_ROOT=https://mirrors.tuna.tsinghua.edu.cn/rustup/rustup' >> ~/.bash_profile
echo 'export RUSTUP_DIST_SERVER=https://mirrors.tuna.tsinghua.edu.cn/rustup' >> ~/.bash_profile
# for fish
echo 'set -x RUSTUP_UPDATE_ROOT https://mirrors.tuna.tsinghua.edu.cn/rustup/rustup' >> ~/.config/fish/config.fish
echo 'set -x RUSTUP_DIST_SERVER https://mirrors.tuna.tsinghua.edu.cn/rustup' >> ~/.config/fish/config.fish
查看合集
📖 Rust web 框架 axum 教程:从入门到遥遥领先
参考
https://github.com/tokio-rs/axum
https://docs.rs/axum/latest/axum/
针对新手的教程 https://github.com/tokio-rs/axum/blob/main/ECOSYSTEM.md#tutorials
tags:
axum
关于作者 🌱
我是来自山东烟台的一名开发者,有敢兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊,
查看更多联系方式
相关文章 🔍
升级 rust 到最新版本
Rust web 框架 axum (五): 每个功能独立一个文件
Rust web 框架 axum (四): 生产服务器环境使用 Nginx 部署 axum 服务
Rust web 框架 axum (三): 使用 askama html 模板
Rust web 框架 axum 教程:从入门到遥遥领先
Rust web 框架 axum (二): 返回 HTML form 表单,并处理 post 请求
计划用 Rust 重构目前的这个博客
使用国内源安装 Rust
Rust web 框架 axum (一): 安装及 hello world (AMP 版)
所有分类
生活
跑步
清单
足球
鲁班
探索
孤独的美食家
驾驶
电视剧
收纳
奶爸
健康
game
电影
周末
Geek
健身
Laravel
Git
Vim
MySQL
Linux
UI
Windows
SVN
纪录片
管理
Shell
游记
工具
手机
BackboneJS
自建博客
Mac
DNS
Tornado
CDN
Django
Python
AngularJS
理财
前端
Nginx
爬虫
Redis
Javascript
Browser
浏览器
推广
OAuth
CSS
PHP
Social Networks
安全
运维
创业
杂记
VueJS
Android
Image
IDE
Java
ReactJS
数据分析
SQLite
RESTful
读书笔记
家电
ecshop
Vagrant
wordpress
docker
SEO
GTD
magento
mongodb
nodejs
weex
冷知识
ruby
iOS
微信小程序
AI
CMS
快应用
backpack
广告联盟
OA
短信
UWP
Win
CSharp
Tampermonkey
graphviz
钉钉
WPS
数据字典
微信公众号
Fuchsia
Adobe XD
SQL Server
thinkphp
代码规范
商业模式
Flutter
头痛的问题
serverless
视频制作
国际化
golang
服务器
Kotlin
网站建设
5G
笔记本
图片
spark
spring
物联网
InfluxDB
图像识别
postgre
rust
成长的烦恼
闲言碎语
待产
不睡觉
写作
程序员
孙心然语录
原则
大鸿语录
地球
植物
时间
中文
赚钱
国家地理
烟台
一生伏首拜阳明
emoji
弟子规
英文
国际贸易
2468
日志
145
分类
21219292
浏览
相关笔记
使用国内源安装 Rust
Rust web 框架 axum 教程:从入门到遥遥领先
Rust web 框架 axum (二): 返回 HTML form 表单,并处理 post 请求
Rust web 框架 axum (三): 使用 askama html 模板
Rust web 框架 axum (四): 生产服务器环境使用 Nginx 部署 axum 服务
关于
阅读清单
头痛的问题清单
博客功能更新日志
热门文章 TOP 榜
孙心然语录
关于壹一的一切
购物清单
应用及工具
大象工具
统计人数小程序
Android 桌面记事 (Google Play)
垃圾评论来源 IP 大集合
股票价格波段计算器
项目报价计算器
人民币大写转换
骄傲地使用
阿里云服务器
NameSilo 域名注册
七牛 CDN
Laravel 5.5
Hedwi 邮件发送平台
回到页首
后台处理耗时: 0.115 秒
@2012-2024 大象笔记 sunzhongwei.com 版本: 1.0.24
山东省烟台市开发区制造
鲁ICP备13002606号-1