Axum and lifetimes ("scoped routes")
⚓ Rust 📅 2025-11-23 👤 surdeus 👁️ 9Td;dr: why does axum require Arc for state?
Similar to threads vs "scoped threads", why can't we use local reference for the state?
Background:
I write a lot of simple endpoints, typically need some state.
So, my routes get an argument which is an Arc.
But, this should be a local refence really, since my service should stop before the state is deallocated.
So, question:
Is there a way to spawn a "scoped endpoint" in axum, which blocks until it is stopped?
Is this just because the axum code simplifies a lot without having reference all over the place? (Which would be a reasonable argument; I skimmed the code and saw already some lifetimes)
I also want to add that I'm happy with axum as it is, this issue appears mostly when on boarding new devs. This question is just me being curious.
7 posts - 3 participants
🏷️ Rust_feed