当前位置:网站首页>Rust:axum learning notes (1) Hello World
Rust:axum learning notes (1) Hello World
2022-07-27 04:18:00 【Good at learning】
axum yes Rust Ecological web Framework rookie , Although the project was established soon , but github Upper star Number exceeded 2.8k, Its bottom layer depends on high performance Tokio,Tokio I won't say much about this product , To borrow You know 《 Explain profound theories in simple language Rust Asynchronous programming Tokio》 Last picture :

Rust Medium Tokio It is almost the performance ceiling of similar frames , and axum stay Tokio Build on , The starting point is on the shoulders of giants .
Let's have one first Hello World How to get started with :
[dependencies]
axum="0.4.3"
tokio = { version = "1.0", features = ["full"] }Copy
After adding the above dependencies , You can code it :
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();
}Copy
After starting , Run in the browser :

Add a few more routes :
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")
}Copy
Be careful :/foo At the same time, it binds GET And POST Method routing . You can test :

边栏推荐
- 288页18万字智能化校园总体设计 目录
- Development of NFT digital collection system: Xiaoyi digital intelligence helps brands launch NFT with one click on the chain
- Ant JD Sina 10 architects 424 page masterpiece in-depth distributed cache from principle to practice pdf
- 2022 operation of simulated examination question bank and simulated examination platform for safety production management personnel of hazardous chemical production units
- 利用LCD1602显示超声波测距
- 11.zuul路由网关
- The real digital retail should have richer connotation and significance
- [Code] sword finger offer 04 search in two-dimensional array
- An online duplicate of a hidden bug
- 288 page 180000 word intelligent campus overall design directory
猜你喜欢

线上一个隐匿 Bug 的复盘
![[MySQL series] MySQL index transactions](/img/85/04eb67471ba1348af2a26bc4621c65.png)
[MySQL series] MySQL index transactions

11. Zuul routing gateway

An online duplicate of a hidden bug

DINO 论文精度,并解析其模型结构 & DETR 的变体

ArrayList与LinkedList区别

Parallel desktop startup virtual machine "operation failed" problem solution

Introduction to JVM principle

What is animation effect? What is the transition effect?

2022年危险化学品经营单位主要负责人复训题库及答案
随机推荐
Notes are a little fun
暑假加餐|有钱人和你想的不一样(第5天)+电力系统潮流仿真(文档和Matlab代码)
Manually build ABP framework from 0 -abp official complete solution and manually build simplified solution practice
细说Hash(哈希)
科目三: 济南章丘三号线
Parallel desktop startup virtual machine "operation failed" problem solution
[semidrive source code analysis] [drive bringup] 41 - LCM drive backlight backlight control principle analysis
Read and understand | how data center supports enterprise digital operation
Use tag tag in golang structure
技术分享 | 需要小心配置的 gtid_mode
What is animation effect? What is the transition effect?
JMeter download and installation
js修改对象数组的key值
11.zuul路由网关
xxx is not in the sudoers file. This incident will be reported
二叉树的坡度
Leetcode:433. minimal genetic change
【无标题】
E-commerce system combined with commodity spike activities, VR panorama continues to bring benefits
Subject 3: Jinan Zhangqiu line 6