mirror of
https://github.com/tokio-rs/axum.git
synced 2024-11-25 08:37:29 +01:00
route.md: Add wildcard example code (#2569)
Co-authored-by: David Pedersen <david.pdrsn@gmail.com> Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
This commit is contained in:
parent
5201798d4e
commit
c57c2847b2
1 changed files with 16 additions and 1 deletions
|
@ -56,7 +56,22 @@ Note that `/*key` doesn't match empty segments. Thus:
|
||||||
- `/*key` doesn't match `/` but does match `/a`, `/a/`, etc.
|
- `/*key` doesn't match `/` but does match `/a`, `/a/`, etc.
|
||||||
- `/x/*key` doesn't match `/x` or `/x/` but does match `/x/a`, `/x/a/`, etc.
|
- `/x/*key` doesn't match `/x` or `/x/` but does match `/x/a`, `/x/a/`, etc.
|
||||||
|
|
||||||
Wildcard captures can also be extracted using [`Path`](crate::extract::Path).
|
Wildcard captures can also be extracted using [`Path`](crate::extract::Path):
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use axum::{
|
||||||
|
Router,
|
||||||
|
routing::get,
|
||||||
|
extract::Path,
|
||||||
|
};
|
||||||
|
|
||||||
|
let app: Router = Router::new().route("/*key", get(handler));
|
||||||
|
|
||||||
|
async fn handler(Path(path): Path<String>) -> String {
|
||||||
|
path
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Note that the leading slash is not included, i.e. for the route `/foo/*rest` and
|
Note that the leading slash is not included, i.e. for the route `/foo/*rest` and
|
||||||
the path `/foo/bar/baz` the value of `rest` will be `bar/baz`.
|
the path `/foo/bar/baz` the value of `rest` will be `bar/baz`.
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue