[SOLVED]At compile time, How to collect some functions and call them?

hello everybody,
I'm new here and in Rust, and I'm trying to write a little game in Rust.

In my situation, I got a lot of initializing functions to call, like:

  • room manager: init_room_mgr,
  • player manager: init_player_mgr,
  • and etc.

as there are too many things like these in games, and they varies a lot, it is not rational to always insert new lines into my bootstrap code like main.rs.

so I've been expecting something like a macro, to mark those init funcs and then call them at once, like:

// in mod1.rs
pub fn init_fn1(){

// in mod2.rs
pub fn init_fn2(){

// in mod3.rs
pub fn init_fn3(){

// in main.rs
pub fn main(){

But I don't know how to code that thing exactly, even after I read the macro section in the book.
so, if there is something I can use from crates.io, or any one can give me a simple example for such a macro?


Something like inventory?

1 Like

aha! that's it! and i found the ctor crate which is its basement.
as ctor has some limits, i'd prefer inventory, though i'm not sure if inventory handled this issue or not.
thank you so much!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.