Skip to main content
Extractor that will get captures from the URL and parse them using serde. Any percent encoded parameters will be automatically decoded. The decoded parameters must be valid UTF-8, otherwise Path will fail and return a 400 Bad Request response.

Type signature

Usage

Single parameter

If the path contains only one parameter, you can omit the tuple:

Multiple parameters with tuple

Multiple parameters with struct

Path segment labels will be matched with struct field names:

Capturing all parameters

You can use HashMap or Vec to capture all path parameters:

Error handling

PathRejection
Returned when path parameters cannot be deserialized into the target type.

Error kinds

The PathRejection type contains an ErrorKind enum with detailed error information:
  • WrongNumberOfParameters - The URI contained a different number of parameters than expected
  • ParseErrorAtKey - Failed to parse a value at a specific key (for structs)
  • ParseErrorAtIndex - Failed to parse a value at a specific index (for tuples)
  • ParseError - Failed to parse a value (for primitives)
  • InvalidUtf8InPathParam - A parameter contained invalid UTF-8 after percent decoding
  • UnsupportedType - Attempted to deserialize into an unsupported type
  • DeserializeError - Custom deserialization error

Customizing rejections

See the customize-path-rejection example for how to provide custom error responses.

Optional path parameters

You can use Option<Path<T>> to allow the same handler to be used in routes with and without parameters:

Percent decoding

Path parameters are automatically percent decoded. For example, %20 becomes a space:

Raw path parameters

For lower-level access without deserialization, use RawPathParams: