Router::new in Hello World
Understand what Router::new does in the minimal Axum hello world example, how it creates the routing table, and why every Axum application starts with it.
When you look at a minimal Axum program, one of the first lines you see is let app = Router::new(). This single line creates the empty routing table that your entire web server will use. Without it, Axum has no way to know which requests go to which functions. This page explains what Router::new is, why it exists, how it works, and the common mistakes beginners make when first encountering it. We stay focused on its role in the hello world example; the surrounding pieces like #[tokio::main], route, and axum::serve are mentioned only as needed and are separate topics of their own.
The Role of Router::new in an Axum Application
Axum is a library that turns incoming HTTP requests into calls to your own Rust functions. That mapping - from a request's path and method to a specific function - is stored inside a Router. Think of a Router as a blank notebook where you will later write down rules like "if someone asks for / with GET, call this function". Router::new simply gives you that blank notebook.
The type is called Router because it routes traffic, just like a postal service routes letters to addresses. When a request arrives, Axum looks through all the rules you added and finds the one that matches. The new function is the standard Rust constructor pattern: it returns an empty instance of the type, ready to be configured.
The Basic Hello World with Router::new
Here is the complete minimal Axum hello world program. The line we care about is let app = Router::new(), but the whole file is shown so you can see where it lives.
// src/main.rs
use axum::{
routing::get,
Router,
};
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/", get(|| async { "Hello, World!" }));
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
.await
.unwrap();
axum::serve(listener, app)
.await
.unwrap();
}
The first line inside main calls Router::new(). This returns an empty Router. Immediately after, we call .route("/", get(...)) on that router. That method adds a rule: for the path / and the HTTP method GET, run the function inside get(...). The router is then passed to axum::serve, which starts listening for incoming connections and uses the router to decide what to do with each request.
If you run this program and open http://127.0.0.1:3000 in a browser, you will see "Hello, World!". The router had one rule, and that rule matched the request you made. Without the initial Router::new() call, there would be nothing to call .route() on - the Router type must exist before you can add rules to it.
Everything Is Working:
If you see "Hello, World!" in the browser after running this program, your router is correctly constructed and serving requests. The empty router created by Router::new successfully received one route and delivered the response.
What the Router::new Line Actually Does
The expression Router::new() is a function call that returns a value of type Router. In Rust, types often provide a new function as a conventional way to create a default, empty instance. Router::new is exactly that: it creates a Router with no routes, no state, and no middleware attached.
A fresh Router does not know about any paths yet. It is not useful by itself because every request would result in a 404 Not Found. But it is the necessary starting point for building a routing table. You take that empty router and build it up with method calls. Each call to .route() returns the same Router with one more rule added. This pattern is called a builder pattern, and Router::new is the starting point of that builder.
Builder Pattern in One Sentence:
A builder pattern lets you start with an empty object and add pieces step by step. Every method returns the object again, so you can keep chaining more methods. Router::new() gives you the empty object to start with.
A Mental Model for Router::new
Imagine you are setting up a small information desk in a large building. Before you can answer any questions, you need a desk to stand behind. Router::new() is like placing that empty desk in the lobby. It does not answer any questions yet - it is just the place where you will later put signs, maps, and a list of which person handles which request.
Every Axum program starts by putting down the desk. Then you add rules: "if someone asks about /, send them to this person." The desk itself does not do the work; it directs people to the right place. The router you get from Router::new is the desk. As you add routes, you attach signs to it. At runtime, Axum uses the desk to look at each incoming request and decide where it should go.
Building a Router in Stages
The hello world example builds the router and adds a route in a single chained expression. But you can also separate the steps. This can help you see exactly what Router::new gives you before anything else is added.
// src/main.rs
use axum::{
routing::get,
Router,
};
#[tokio::main]
async fn main() {
// Step 1: create an empty router
let mut app = Router::new();
// Step 2: add a route
app = app.route("/", get(|| async { "Hello, World!" }));
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
.await
.unwrap();
axum::serve(listener, app)
.await
.unwrap();
}
Here we declare app as mut because we reassign it in step 2. The first assignment holds only an empty router. The second assignment replaces it with a router that contains one route. This shows that Router::new is purely the creation of an empty routing table; all actual routing logic comes later.
If you run this version, the result is identical to the chained version. The only difference is readability and the possibility of adding conditions or loops between steps.
Adding Multiple Routes Starting from the Same Router::new
A router can hold many rules. Starting from Router::new, you can chain several .route() calls to build a more complete mapping. The following example adds three different endpoints.
// src/main.rs
use axum::{
routing::get,
Router,
};
async fn index() -> &'static str {
"Home page"
}
async fn about() -> &'static str {
"About page"
}
async fn contact() -> &'static str {
"Contact page"
}
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/", get(index))
.route("/about", get(about))
.route("/contact", get(contact));
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
.await
.unwrap();
axum::serve(listener, app)
.await
.unwrap();
}
The first line is still Router::new(). Each subsequent .route() call adds one more rule to that same router. Notice that we pass named async functions instead of inline closures. Both styles work with the router; the router only cares that the function implements the correct handler trait. For this page, the key point is that every rule lives inside the same Router created by Router::new.
Forgetting to Add Routes:
If you call Router::new() and then pass that empty router to axum::serve without ever calling .route(), every request will return a 404 Not Found. The server will run, but it will not respond with anything useful. Always add at least one route before serving.
Understanding the Type of Router::new
In Rust, Router::new() returns a Router<()>. The angle brackets contain a type parameter, which in this case is the unit type (). This type parameter is used later for shared application state. For now, you can ignore it completely. The important thing is that Router::new returns a Router, and that value can be passed to axum::serve.
The fact that Router::new() returns a concrete type that can be passed to serve is one reason the hello world example compiles and runs. axum::serve expects a router - specifically a Router with no missing state - and the one produced by Router::new satisfies that expectation out of the box. You do not need to annotate any types; Rust infers everything.
Trying to Serve Something Else:
axum::serve requires a router, not a string, a function, or a random object. If you forget to call Router::new() and try to pass something else, the compiler will reject your program with a type error. The error message will point out that the argument does not implement the required Service trait. Always start with let app = Router::new() and build from there.
Common Misconceptions About Router::new
Beginners often assume that Router::new() creates a router that already knows some default routes. It does not. There is no automatic / route, no built-in health check, no default 404 handler. An empty router responds with 404 to everything. Even the 404 response is generated by Axum's default fallback, not by anything inside the router itself.
Another misconception is that Router::new starts a server or opens a network connection. It does not. It only builds an in-memory data structure. The server is started later by axum::serve, which takes the router as input. You can create a router, pass it to a function, clone it, or store it in a variable without any network activity happening.
A third mistake is thinking that Router::new must be called inside an async function or inside #[tokio::main]. It does not. The function is synchronous and can be called anywhere. However, in the hello world example it is convenient to call it inside main right before serving because that keeps the whole application setup in one place.
How Router::new Fits into the Bigger Picture
Every Axum application follows the same pattern: create a router, add routes, create a listener, and serve. Router::new is the first step. Later, when you learn about extractors, state, and middleware, you will still begin with Router::new() and then chain additional methods like .with_state() or .layer(). Those methods are not part of this page, but they all operate on the same Router value you get from new.
In real-world Axum projects, you will often see a function dedicated to building the router, like this:
async fn index() -> &'static str {
"Home page"
}
async fn health_check() -> &'static str {
"OK"
}
fn create_router() -> Router {
Router::new()
.route("/", get(index))
.route("/health", get(health_check))
// more routes later
}
The Router::new() call is always the foundation. It is the same in a hello world and in a production application. That consistency is one of the things that makes Axum approachable.
Practical Example: Starting with an Empty Router and Observing 404
To really understand what Router::new gives you, try running a program that creates a router but adds no routes.
// src/main.rs
use axum::Router;
#[tokio::main]
async fn main() {
let app = Router::new();
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
.await
.unwrap();
axum::serve(listener, app)
.await
.unwrap();
}
This program compiles and runs. Open http://127.0.0.1:3000 and you will get a 404 Not Found response with an empty body - Axum sends no page of its own; what your browser displays for it is the browser's own error page. The server is working, but the router has no rules, so every request ends in "not found". That is exactly what an empty router should do. It proves that Router::new only creates the container; the routing logic comes from adding routes.
After you see the 404, stop the server and add the route from the first example. The contrast makes the purpose of Router::new clear: it is the empty shell you fill with matching rules.
Don't Panic About 404:
Seeing a 404 from a router you just created but forgot to add routes to is not a bug in Axum. It is the correct behavior of an empty routing table. The fix is to add at least one route using .route().
Alternative Ways to Create a Router
The standard and recommended way is Router::new(). You will see this in every official Axum example. There is no other widely used constructor for a basic router. The Router type does implement the Default trait, so you could write Router::default(). That produces exactly the same empty router. However, Router::new is the conventional name and the one used in all documentation. Stick with Router::new unless you have a specific reason to use Default.
There is also Router::new().with_state(...) for adding shared state, but that is a different topic. For the hello world stage, Router::new() on its own is all you need.
Summary
Router::new() creates an empty routing table. It is the first building block of every Axum application. You start with that empty router, add routes with .route(), and then pass the finished router to axum::serve. The router does not start a server, does not include any default routes, and does not require async. Its only job is to be the container that holds the mapping from incoming requests to your handler functions.
Once you understand that Router::new is just an empty notebook waiting for your routing rules, the rest of Axum becomes much easier to follow. The next logical step in learning Axum is to explore the .route() method in detail - how it adds rules to the router you just created, and how different HTTP methods are handled.