How to manage multiple context structs

How do you usually manage in RUST the context that you need to keep around?

I'm often in a situation like this:

fn main() {
    let a = A::new(); // Maybe some data provider from disk
    let b = B::new(); // Maybe some data provider from web
    let c = C::new(); // Maybe some struct for the CLI (i.e. rustyline)

    do_something_useful(&A, &mut B, &C);

Of course this is not nice. But what is the best alternative here?

I could simply create a new Context struct to hold all the other structs and just pass that around but having for example B as &mut is not making things much nicer either.

So, how do you usually manage this case?

I like to have a struct Context(Arc<ContextInner>)—or struct Context(Arc<RwLock<ContextInner>) in case the context is mutable—to pass the app state/execution context around. I usually work with async stuff so passing references around is a no no which is why I prefer the newtype around Arc and a ContextInner which takes ownership of a, b and c. Arc is also inexpensive to clone.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.