I am currently implementing my first library in Rust, and as it is the case with many developers coming from other languages like Java etc. I am struggling to find right solutions for common patterns/use cases in Rust.
My Rust library has multiple structs. Many of them need to have access to some common configuration options which are essential for their function. For example:
pub struct A {
pub context: Context
}
pub struct B {
pub context: Context
}
pub struct Context {
pub dbUri: String
}
The context will be passed to each struct once when the struct is initialized. The context attribute values might change at runtime which should make these changes available to any dependent struct. Structs should be able to make changes to context if required. It might be required to make the context available to different threads in the future.
The best solution I could find so far is to use Mutex wrapped in Arc as described here.
I would like to know whether this is the right way to implement my requirement, and what are other available options?