Beginner Guidance on Dependency Injections and Globals
⚓ Rust 📅 2025-09-26 👤 surdeus 👁️ 8Hello everyone,
I'm a bit new to Rust and would like some advice on the best way to handle shared dependencies between functions.
I am writing a single-threaded application that needs to use some global state in different parts of the program. In particular, I am sharing an HTTP client and a HashMap that stores a mapping of hostnames to IP addresses and other hostnames.
My first instinct was to pass the dependencies around in Rc<T> as arguments, but this became harder to manage as I added more dependencies, requiring me to update all usages of each function or struct that used them. I did some research and found that I could possibly pass an Rc'd context struct and use RefCell<T> on the members. Another option was using some kind of dependency injection library with macros that would auto-wire things for me.
I work primarily in Go, where using traditional dependency injection (either runtime or codegen) is commonplace. I'm wondering what the "canonical" way of dealing with this problem is.
Thanks in advance!
3 posts - 2 participants
🏷️ Rust_feed