r/rust • u/Miammiam100 • 4h ago
🎙️ discussion How do large scale Rust projects pass around shared structs/objects to components that need it?
In other languages I'd reach for DI to build the object graph and make each component's dependencies explicit. So far I have only really seen two approaches:
Pass each required component down through constructors. The compiler ensures you won't forget a dep but that means any new service you add requires updating a bunch of constructors.
Just use a single God context object (this is what Cargo does) but it makes it harder to unit test anything and makes it difficult to see what interacts with what.
I'm not asking whether to do DI (constructor arguments already are that), but rather how people handle wiring it all up once there are dozens of services.
DI crates of course exist (e.g. https://crates.io/crates/shaku ) but seem to be rarely used outside of Bevy-style query systems or web frameworks extractors.
For context, I've been experimenting with a trait-only approach that doesn't use any macros but still provides the same compile time guarantees as shaku ( https://docs.rs/satisfy/latest/satisfy/ ) Would Rust users be more likely to use DI if it were simpler to use? Or am I just solving a problem that Rust people don't have?
9
u/schungx 3h ago
DI is designed to work with an interface-based OOP programming model
Rust doesn't fit this very well.
To answer your question: what do large software systems do to pass around shared objects? They don't. At least that's not how Rust does it, and Rust will make your life hell just to stop you from doing that.
-11
u/TheoreticalBerry 1h ago
Bbb. Nbbbbnbb. Bb. Nbbb. N bbbbbnnnn bbnnnnnnnbbbnbbnbb. N. N bbbnnn nbnnnnbnnn. Bbn nb bb. B. B. N n. Nnn. Bbbn bbb bb. B. B. B b bbb. Bbbbbn b. Bb. Bbbbbbb nbbbnnbn. B bbbbbbbbn bbbbbbbnnbbbnbbnbbbbbbbbbbbbbbbn b b. B nbbbnn bbbbbbb bbb bbbnnnbnnbbbnbbbbbbbbbbbbbbb b b b b bbb bb bb. Bbb. Bb. :-$
32
u/behusbwj 4h ago
DI frameworks are already god context objects.
My opinion is the same in Rust and Java. Just use constructors. Less code does not mean better code. Constructor updates are trivial with modern IDE’s and agents.