Multiple Segments

You can also match against multiple segments by using <param..> in a route path. The type of such parameters, known as segments parameters, must implement FromSegments. Segments parameters must be the final component of a path: any text after a segments parameter will result in a compile-time error.

As an example, the following route matches against all paths that begin with /page/:

  1. #[get("/page/<path..>")]
  2. fn get_page(path: PathBuf) -> T { ... }

The path after /page/ will be available in the path parameter. The FromSegments implementation for PathBuf ensures that path cannot lead to path traversal attacks. With this, a safe and secure static file server can be implemented in 4 lines:

  1. #[get("/<file..>")]
  2. fn files(file: PathBuf) -> Option<NamedFile> {
  3. NamedFile::open(Path::new("static/").join(file)).ok()
  4. }