当前位置:网站首页>Rust:axum学习笔记(1) hello world
Rust:axum学习笔记(1) hello world
2022-07-27 03:04:00 【爱学习的佳】
axum是Rust生态的web框架新秀,虽然项目成立不久,但github上的star数已超2.8k,其底层依赖的是高性能的Tokio,Tokio这货就不多说了,借用知乎《深入浅出Rust异步编程之Tokio》上的一张图:

Rust中的Tokio几乎是同类框架的性能天花板了,而axum在Tokio基础上构建,起点就站在巨人的肩膀上。
先来一个Hello World的入门示例:
[dependencies]
axum="0.4.3"
tokio = { version = "1.0", features = ["full"] }复制
添加上面的依赖项后,就可以编码了:
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();
}复制
启动后,浏览器里跑一下:

再加几个路由:
use axum::{routing::get, Router};
#[tokio::main]
async fn main() {
// our router
let app = Router::new()
.route("/", get(root))
.route("/foo", get(get_foo).post(post_foo))
.route("/foo/bar", get(foo_bar));
// run it with hyper on localhost:3000
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}
// which calls one of these handlers
async fn root() -> String {
String::from("hello axum")
}
async fn get_foo() -> String {
String::from("get:foo")
}
async fn post_foo() -> String {
String::from("post:foo")
}
async fn foo_bar() -> String {
String::from("foo:bar")
}复制
注意:/foo同时绑定了GET及POST方法的路由。可以测试一下:

边栏推荐
- mysql中case when返回多个字段处理方案
- Leetcode daily exercise: sort sentences
- 括号的最大嵌套深度
- An online duplicate of a hidden bug
- A. YES or YES?
- C language learning notes - memory management
- H.265网页播放器EasyPlayer对外开放录像的方法
- Is VR panoramic production a single weapon in the home decoration industry? Why is this?
- C. Cypher
- Abstract intelligent extraction [based on Bert technology]
猜你喜欢

list模拟实现

Restful Fast Request 2022.2.2发布,支持批量导出文档

First pass of routing strategy

356页14万字高端商业办公综合楼弱电智能化系统2022版

C语言学习笔记 —— 内存管理

Maximum subarray cumulative sum less than or equal to K

Summer meal | rich people are different from what you think (day 5) + power system power flow simulation (documents and matlab code)

Want to get the Apache official domain name mailbox? Exclusive interview with Apache linkis five new committers to tell you how to do it

什么是动画效果?什么是过渡效果?

Subject 3: Jinan Zhangqiu line 3
随机推荐
A. YES or YES?
【比赛参考】PyTorch常用代码段以及操作合集
Interview question 16.05 factorial mantissa
Development of NFT digital collection system: Xiaoyi digital intelligence helps brands launch NFT with one click on the chain
基于风能转换系统的非线性优化跟踪控制(Matlab代码实现)
【OBS】circlebuf
What is the principle difference between lateinit and lazy in kotlin
Is VR panorama just needed now? After reading it, you will understand
Lixia action | Yuanqi Digitalization: existing mode or open source innovation?
Session&Cookie&token
mysql中case when返回多个字段处理方案
Bean Validation原理篇--07
记一次TCP丢包带来的重大性能问题
Redis database, which can be understood by zero foundation Xiaobai, is easy to learn and use!
leetcode:433. 最小基因变化
手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践
CloudCompare&PCL 匹配点距离抑制
Internet of things smart home project - Smart bedroom
科目三: 济南章丘五号线
搜索旋转排序数组