MatchedPath is an extractor that provides the route pattern (not the actual request path) that matched the current request.
Basic usage
The matched path is the route pattern (e.g.,
/users/{id}), not the actual request URI (e.g., /users/123). Use Uri or OriginalUri to get the actual request path.Using in middleware
MatchedPath is commonly used in middleware for logging, metrics, and tracing:
Nested routes
When using nested routers,MatchedPath includes the full pattern from all nesting levels:
Optional extraction
MatchedPath won’t be available in fallback handlers or when using nest_service. Use Option<MatchedPath> to handle these cases:
Type signature
&str
Returns a string representation of the matched route pattern.
Rejection
Rejects withMatchedPathMissing if:
- The request didn’t match any route (fallback handler)
- Used with
nest_serviceinstead ofnest - Extracted before routing has occurred
Common use cases
- Structured logging: Group logs by route pattern instead of specific paths
- Metrics collection: Track request counts and latencies per route
- Rate limiting: Apply different rate limits based on route patterns
- Debugging: Understand which route handler is processing a request
Example: Metrics middleware
See also
- Uri - Extract the actual request URI
- OriginalUri - Extract the original URI before nesting modifications