路由和处理函数
- 需要启用 tokio 的
macros和rt-multi-thread或full启用所有功能(cargo add tokio --features macros,rt-multi-thread)
toml
[package]
name = "helloRust"
version = "0.1.0"
edition = "2024"
[dependencies]
axum = "0.8.4"
tokio = { version = "1.46.1", features = ["full"] }路由
rust
use axum::{
routing::get,
Router,
};
#[tokio::main] // 用宏捏
async fn main() {
// 多整点路由捏
let app = Router::new()
.route("/", get(root))
.route("/foo", get(get_foo).post(post_foo))
.route("/foo/bar", get(foo_bar));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
// 调用对应函数捏
async fn root() -> &'static str {"hey bro"}
async fn get_foo() -> &'static str {"what can i say"}
async fn post_foo() -> &'static str {"ohhohoboboboboooo"}
async fn foo_bar() -> &'static str {"整点无聊的?"}