optional path parameter #2816
-
how can I do something like this: #[get("/times/<level>/<amount>")]
fn times(level: &str, amount: Option<u32>) |
Beta Was this translation helpful? Give feedback.
Answered by
the10thWiz
Jul 2, 2024
Replies: 1 comment
-
This isn't possible with the current macros. ( You will have to mount two different routes, one with and one without the final parameter. To reduce code duplication, I would recommend creating a helper that actually implements the logic, and each route simply calls the helper. fn times(level: &str, amount: Option<u32>) { ... }
#[get("/times/<level>")]
fn times_one(level: &str) {
times(level, None)
}
#[get("/times/<level>/<amount>")]
fn times_two(level: &str, amount: u32) {
times(level, Some(amount))
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Swarkin
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This isn't possible with the current macros. (
Option<u32>
will returnSome(...)
if the value is a validu32
, andNone
if it isn't a validu32
- e.g.abc
would be parsed toNone
)You will have to mount two different routes, one with and one without the final parameter. To reduce code duplication, I would recommend creating a helper that actually implements the logic, and each route simply calls the helper.