Precedence
When multiple routes can match the same request, Mach uses route precedence to choose the most specific match.
Static Segments
Section titled “Static Segments”Static route segments always take precedence over route parameters.
For example:
app.mapGet("/users/profile", [] { return mach::ok("Profile");});
app.mapGet("/users/{name:string}", [](mach::Context& context) { const auto name = context.request.routeParam("name"); return mach::ok(name);});For this request:
GET /users/profileMach selects:
/users/profilerather than:
/users/{name:string}This allows specific routes and dynamic routes to coexist naturally.
Constraint Precedence
Section titled “Constraint Precedence”When constrained parameters overlap, more specific constraints take precedence.
The int constraint takes precedence over the string constraint.
For example:
app.mapGet("/users/{name:string}", [](mach::Context& context) { const auto name = context.request.routeParam("name"); return mach::ok(name);});
app.mapGet("/users/{id:int}", [](mach::Context& context) { const int id = context.request.routeParam<int>("id"); return mach::ok(id);});For:
GET /users/42Mach selects:
/users/{id:int}For:
GET /users/aliceMach selects:
/users/{name:string}Ambiguous Routes
Section titled “Ambiguous Routes”Mach rejects route definitions that are ambiguous with an already registered route.
For example, these routes describe the same integer route shape:
/users/{id:int}/users/{number:int}The parameter names are different, but both routes match the same requests. Registering both for the same HTTP method results in an invalid route definition.
Mach also rejects other route definitions whose matching behavior would be ambiguous.
This keeps route selection deterministic and prevents registration order from being used to resolve fundamentally ambiguous route definitions.
Precedence Summary
Section titled “Precedence Summary”When matching a request, the important precedence rules are:
- Static segments take precedence over parameter segments.
intparameters take precedence overstringparameters.- Ambiguous route definitions are rejected when routes are registered.
You’ve now covered Mach’s routing system: defining routes, using parameters and constraints, and understanding how routes are matched.