I was playing around with axum while learning Rust, and wanted to try a change where I create the Router in one place but define the routes in another module. Since it failed spectacularly I thought about doing something easier first like configure the routes in the same module just a different function. No matter what I tried, passing mutable or reference Rust just doesn't allow it.
I just want to be allowed to configure the routes separately from the place I create the router. Here is my latest attempt:
use axum::{
extract::Path,
http::StatusCode,
routing::{get, post},
Json, Router,
};
use serde::{Deserialize, Serialize};
use serde_json::to_string;
use tokio;
async fn root_handler() -> &'static str {
"Hello, world!"
}
pub fn configure_routes(app: &mut Router) {
app.route("/", get(root_handler));
}
#[tokio::main]
async fn main() {
let mut app = Router::new();
configure_routes(&mut app);
// run our app with hyper, listening globally on port 3000
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
Why doesn't this work? And how can I configure the routes separately?
Router::route()consumesselfand returns it, so you need to do the same: