Dependency injection in Rust often ends up as a global OnceLock plus a few Arc<Mutex<...>> values.
That is not because the teams do not understand lifetimes.
It is because the standard library has no container, and the easiest thing that compiles is a singleton.
The problem with a singleton is not performance. It is that it conflates creation time with ownership.
A connection pool, a request context, and a cache client do not have the same lifetime.
When they are all process-wide, the first concurrent bug is usually a DbContext shared across two requests.
Three lifetimes, one rule
rust-dix uses the Microsoft.Extensions.DependencyInjection model: singleton, scoped, transient.
| Lifetime | One instance per | Typical use | Common mistake |
|---|---|---|---|
| Singleton | process | config, pool, cache client | registering DbContext as singleton |
| Scoped | request or task | DbContext, request context | resolving it from the root |
| Transient | resolution | stateless helpers | giving state to a transient |
The rule that matters is strict scopes: resolving a scoped service from the root provider returns a typed error, ScopedFromRootProvider.
It does not hand you a root-owned instance and hope you remember.
Registering is easy; resolving is the design
Registration is a builder call:
let mut services = ServiceCollection::new(); services.singleton(|_| Arc::new(AppConfig::load()?)); services.scoped(|sp| { let options = sp.resolve_arc::<DbContextOptions>()?; Ok(Arc::new(DbContext::new(options))) }); The factory uses resolve_arc, not get. A missing dependency is a typed error at build or resolution time, not a panic and not a silent None.
For a web request, the handler asks for an owned DbContext:
#[derive(Inject)] pub struct CancelOrderHandler { #[inject(owned)] pub ctx: DbContext, #[inject] pub clock: Arc<dyn IClock>, } For a background worker, there is no request scope. The worker injects Arc<dyn ScopeFactory> and creates one scope per tick:
let scope = self.scopes.create_scope(); let mut ctx = scope.get_owned::<DbContext>()?; // one tick of work drop(scope); // releases scoped services in reverse order The important part is not the syntax. It is that the code has to say who owns the context.
What it costs
Strictness moves the thinking earlier. That is not free.
A CLI or a small script does not benefit from a container.
The lifetime rules become noise, and rust-dix provides a lenient mode for debugging, but it should not reach production.
Async initialization is another edge. A provider that needs an async connection must go through build_async or an async factory.
Calling block_on inside a synchronous factory tends to hang the startup rather than return a business error.
There is also a boundary around where the container is built. It belongs in the host composition root, not inside a library crate.
A library that constructs its own ServiceCollection reverses the dependency direction and makes tests harder to isolate.
| Cost | What it looks like | Mitigation |
|---|---|---|
| Earlier design work | you decide ownership before writing the handler | map lifetimes to layers |
| Async initialization | block_on in a sync factory hangs startup | use build_async / async factory |
| Container placement | a library builds its own collection | build it in the composition root |
None of these is a reason to avoid a container. They are reasons to use one only when the lifetime question is real.
A five-minute check
If you are cleaning up global singletons, do the smallest experiment.
Register DbContext as scoped. Change one handler to #[inject(owned)]. Then deliberately resolve DbContext from a singleton.
If you get ScopedFromRootProvider, the constraint is doing its job. If you do not, either the registration is wrong or the scope is not the one you think it is.
That error is more useful than a comment saying "do not share this".
Repository: https://github.com/Rust-Framework/rust-dix
