Use Rust macro for "registration"

47 views Asked by At

Problem

Is it possible to use procedural macros in Rust to effectively register that a function (or something else) exists?

Example

As an example, let's pretend I'm creating a web framework that has several routes and route handlers. I could define the handlers as functions with macros, then the router automatically knows about them:

#[get("/hello")]
fn hello() -> &'static str {
    "Hello, world!"
}

#[get("/world")]
fn world() -> &'static str {
    "Hello, world!"
}

fn main() {
    let server = Server::new();
    while true {
        server.listen();
    }
}

Notice how in this example, the functions hello and world aren't visibly called anywhere.

0

There are 0 answers