How to set static variable that require async initialization

Hi,

I'm new to Rust and trying to learn it by doing some small demo projects. Currently, I'm working on the Todo REST API example using Actix-web and SQLx crates. So far I have made it works well enough by creating SQLx pool inside of main and share it trough App::new().data(db_pool.clone()). Then I can access it through action handlers by using db_pool: web::Data<PgPool> and pass it to models where I can use it for database access.
It all works well, but I'm still not happy with it. I will prefer it if I don't need to pass it to each action handler and instead make it somehow accessible directly to models, for example like importing "db" module and use it like db::DB or something like that... I have found lazy_static crate and it looks exactly like something I wish to have, but it doesn't support async initialization...
After the whole weekend spent on trying to find some way to do it I just wish to check is it possible to do it that way, or I just keep using the app to share it?

Thanks for any help and suggestion...

So, generally you can't, without some placeholder value to exist in there until it is initialized.

For async I've found https://lib.rs/crates/double-checked-cell-async

However, global variables are generally problematic. Try initializing whatever you need first, and then passing it to functions that need it.

Thanks for explaining. When I have thought about that again, it was bad idea... thanks :slight_smile: