liveBook · Manning

Path extractor

As explained in Rust Servers, Services, and Apps

A path extractor is an Actix Web request extractor that reads typed values from URL path parameters.

A path extractor reads typed values from URL path parameters and supplies them to an Actix Web handler. Routes declare placeholders such as {tutor_id} and {course_id}; handlers receive those values through web::Path, while request bodies use separate JSON extractors. section 6.2.2

Match the extractor type to the route parameters:

  • Use web::Path<i32> for a route containing one identifier, then call into_inner() to obtain the integer. section 6.2.2
  • Use web::Path<(i32, i32)> for nested identifiers such as tutor_id and course_id, then destructure the tuple. section 6.2.2
  • Pass extracted identifiers to database access functions, which can return successful responses or not-found errors. section 6.2.2

Extracting two resource identifiers

pub async fn get_course_details( app_state: web::Data<AppState>, path: web::Path<(i32, i32)>, ) -> Result<HttpResponse, EzyTutorError> { let (tutor_id, course_id) = path.into_inner(); get_course_details_db(&app_state.db, tutor_id, course_id).await .map(|course| HttpResponse::Ok().json(course)) } section 6.2.2

pub async fn get_course_details(
    app_state: web::Data<AppState>,
    path: web::Path<(i32, i32)>,
) -> Result<HttpResponse, EzyTutorError> {
    let (tutor_id, course_id) = path.into_inner();
    get_course_details_db(&app_state.db, tutor_id, course_id)
        .await
        .map(|course| HttpResponse::Ok().json(course))
}

Inspect the complete handler pattern: receive web::Path, unpack both IDs, call persistence logic, and serialize the result. section 6.2.2

How Rust Servers, Services, and Apps covers it

How deep it goes
Shows scalar and tuple extraction with into_inner(), but defers extractor internals and custom validation.
Examples
Uses tutor and course identifiers in web::Path<i32> and web::Path<(i32, i32)> handler examples.
What it assumes
Assumes familiarity with Rust handlers, Actix Web routes, and asynchronous database calls.
What you can do after
Apply it via Actix Web to read typed identifiers inside asynchronous request handlers.
How it connects
Connects routing with handlers, HTTP methods, JSON payloads, database access, and error handling.

This explainer was generated by Manning's knowledge graph from the text of Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.