2

Below is the "Hello world" example of Rocket, which only has one route handler (hello()):

#[macro_use] extern crate rocket;

#[get("/hello/<name>/<age>")]
fn hello(name: &str, age: u8) -> String {
    format!("Hello, {} year old named {}!", age, name)
}

#[launch]
fn rocket() -> _ {
    rocket::build().mount("/", routes![hello])
}

But in a typical web application there can be hundreds of handler-functions and they all need to be added to routes![]. Let's say we want to map them all to "/". I was wondering whether it's possible in Rust to avoid that repetitive task. So is it possible to either change/extend the behavior of the existing macros directly or create some sort of wrappers for that purpose?

Note: I am not asking why Rocket doesn't support that. I read the author of Rocket (SergioBenitez) does not think such a feature would be a good idea. My question is about whether such a thing would be technically possible in Rust and if so, how it could be done.

kmdreko
  • 42,554
  • 6
  • 57
  • 106
at54321
  • 8,726
  • 26
  • 46

0 Answers0