创建我们的rust项目
cargo new axum-demo01
在cargo.toml引入axum的库和tokio库
[dependencies]
axum ={ version = "0.6"}
tokio ={ version = "1",features = ["full"]}
在src目录下的main.rs中编写代码
use axum::{
routing::get,
Router,
};
#[tokio::main]
async fn main() {
// 路由
let app = Router::new().route("/", get(|| async { "Hello, World!" }));
//0.0.0.0 表示所有ip都可以访问,3000是访问端口
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}
使用浏览器访问127.0.0.1:3000