Using `Rc` with `lazy_static`?

Is there a way to create a static Rc variable? Only Arc seems to work:

lazy_static! {
    static ref MY_STRING: Arc<String> = Arc::new(String::from("Hello"));
}

I now have to use Arc everywhere (due to how type usage propagates “virally”). Is that a problem? It seems unnecessary for my single-threaded code.

Statics are globals, so they can be accessed by multiple threads at the same time. Using Rc would be unsound. If you want a distinct copy per thread, use thread_local! instead.

8 Likes