An extractor for named path segments
Allows routes to access named path segments ({foo}
). Each Named<T>
extracts a single
segment. T
must implement the NamedSegment
trait - to provide the segment name - and the
FromStr trait. Fails with a BAD_REQUEST
response if the segment is not found, fails to
parse or if multiple identically named segments exist.
Extracting a Number
from a named path segment with:
use tide::head;
use tide::head::{Named, NamedSegment};
struct Number(i32);
impl NamedSegment for Number {
const NAME: &'static str = "num";
}
impl std::str::FromStr for Number {
type Err = std::num::ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
s.parse().map(|num| Number(num))
}
}
async fn named_segments(Named(number): Named<Number>) -> String {
let Number(num) = number;
format!("number: {}", num)
}
fn main() {
let mut app = tide::App::new(());
app.at("/path_named/{num}").get(named_segments);
app.serve()
}
Attempt to extract a value from the given request.
Mutably dereferences the value.
The resulting type after dereferencing.
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static