pub struct Path<T>(pub T);Expand description
Path parameter extractor.
Extracts path parameters from the URL and deserializes them to type T.
§Supported Types
- Single value:
Path<i64>extracts the first (or only) path parameter - Tuple:
Path<(String, i64)>extracts parameters in order - Struct:
Path<MyParams>extracts parameters by field name
§Error Responses
- 500 Internal Server Error: Path parameters not set by router (server bug)
- 422 Unprocessable Entity: Parameter missing or type conversion failed
§Examples
§Single Parameter
ⓘ
#[get("/users/{id}")]
async fn get_user(Path(id): Path<i64>) -> impl IntoResponse {
format!("User ID: {id}")
}§Multiple Parameters (Tuple)
ⓘ
#[get("/users/{user_id}/posts/{post_id}")]
async fn get_post(Path((user_id, post_id)): Path<(i64, i64)>) -> impl IntoResponse {
format!("User {user_id}, Post {post_id}")
}§Struct Extraction
ⓘ
#[derive(Deserialize)]
struct PostPath {
user_id: i64,
post_id: i64,
}
#[get("/users/{user_id}/posts/{post_id}")]
async fn get_post(Path(path): Path<PostPath>) -> impl IntoResponse {
format!("User {}, Post {}", path.user_id, path.post_id)
}Tuple Fields§
§0: TImplementations§
Source§impl<T> Path<T>
impl<T> Path<T>
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Unwrap the inner value.
Trait Implementations§
Source§impl<T: DeserializeOwned> FromRequest for Path<T>
impl<T: DeserializeOwned> FromRequest for Path<T>
Source§type Error = PathExtractError
type Error = PathExtractError
Error type when extraction fails.
Source§async fn from_request(
_ctx: &RequestContext,
req: &mut Request,
) -> Result<Self, Self::Error>
async fn from_request( _ctx: &RequestContext, req: &mut Request, ) -> Result<Self, Self::Error>
Extract a value from the request. Read more
impl<T: Copy> Copy for Path<T>
Auto Trait Implementations§
impl<T> Freeze for Path<T>where
T: Freeze,
impl<T> RefUnwindSafe for Path<T>where
T: RefUnwindSafe,
impl<T> Send for Path<T>where
T: Send,
impl<T> Sync for Path<T>where
T: Sync,
impl<T> Unpin for Path<T>where
T: Unpin,
impl<T> UnwindSafe for Path<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Instruments this future with a span (no-op when disabled).
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Instruments this future with the current span (no-op when disabled).