Skip to content

optional path parameter #2816

Closed Answered by the10thWiz
Swarkin asked this question in Questions
Discussion options

You must be logged in to vote

This isn't possible with the current macros. (Option<u32> will return Some(...) if the value is a valid u32, and None if it isn't a valid u32 - e.g. abc would be parsed to None)

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))
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Swarkin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants