How to use axum crate in Rust web framework

Set up an Axum web server in Rust by adding dependencies and defining a route handler with Tokio.

Use axum by adding it to your Cargo.toml alongside tokio and defining a handler function that returns a response.

use axum::{routing::get, Router};

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(handler));
    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

async fn handler() -> &'static str {
    "Hello, World!"
}

Add these dependencies to Cargo.toml:

[dependencies]
axum = "0.7"
tokio = { version = "1.51.1", features = ["full"] }