# Lazy\_static! native

**URL:** <https://internals.rust-lang.org/t/lazy-static-native/20372>\
**Category:** language design\
**Created:** [February 22, 2024, 10:30pm UTC](https://internals.rust-lang.org/t/lazy-static-native/20372 "2024-02-22T22:30:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rxdiscovery](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rxdiscovery/32/11755_2.png) [@rxdiscovery](https://internals.rust-lang.org/u/rxdiscovery)\
**Post date:** [February 22, 2024, 10:30pm UTC](https://internals.rust-lang.org/t/lazy-static-native/20372/1 "2024-02-22T22:30:33Z")

</div>

Hello,

I've noticed that for a large majority of the projects I've coded in Rust, I've needed to use shared globals statics data. And the only way to do this safely is to use Crate **lazy\_static**.

Ex:

```rust
lazy_static! {
    static ref TOKIO_RUNTIME: tokio::runtime::Runtime = tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .expect("TOKIO RUNTIME ERROR");
    static ref ACTIONS_QUEUE: Arc<Mutex<VecDeque<WaitingAction>>> = Arc::new(Mutex::new(VecDeque::new()));
}

```

Wouldn't it be easier to do this natively in Rust without having to use a Crate (and therefore a compile-time cost to interpret the Macros)?

---

<div class="post-metadata">

**Author:** ![chrefr](https://avatars.discourse-cdn.com/v4/letter/c/e480ec/32.png) [@chrefr](https://internals.rust-lang.org/u/chrefr)\
**Post date:** [February 22, 2024, 10:33pm UTC](https://internals.rust-lang.org/t/lazy-static-native/20372/2 "2024-02-22T22:33:26Z")

</div>

[2788-standard-lazy-types - The Rust RFC Book](https://rust-lang.github.io/rfcs/2788-standard-lazy-types.html), which [is already implemented](https://doc.rust-lang.org/nightly/std/sync/struct.LazyLock.html), and [part of it is already stabilized](https://doc.rust-lang.org/nightly/std/sync/struct.OnceLock.html).

---

<div class="post-metadata">

**Author:** ![chrefr](https://avatars.discourse-cdn.com/v4/letter/c/e480ec/32.png) [@chrefr](https://internals.rust-lang.org/u/chrefr)\
**Post date:** [February 22, 2024, 10:36pm UTC](https://internals.rust-lang.org/t/lazy-static-native/20372/3 "2024-02-22T22:36:15Z")

</div>

Also note that there is no need to use `Arc` in `static`, and [`Mutex::new()`](https://doc.rust-lang.org/stable/std/sync/struct.Mutex.html#method.new) and [`VecDeque::new()`](https://doc.rust-lang.org/stable/std/collections/struct.VecDeque.html#method.new) are already `const`, so the second `static` can be a normal static:

```rust
static ACTIONS_QUEUE: Mutex<VecDeque<WaitingAction>> = Mutex::new(VecDeque::new());

```

---

<div class="post-metadata">

**Author:** ![rxdiscovery](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rxdiscovery/32/11755_2.png) [@rxdiscovery](https://internals.rust-lang.org/u/rxdiscovery)\
**Post date:** [February 22, 2024, 10:39pm UTC](https://internals.rust-lang.org/t/lazy-static-native/20372/4 "2024-02-22T22:39:51Z")

</div>

> [@chrefr](#):
>
> [2788-standard-lazy-types - The Rust RFC Book](https://rust-lang.github.io/rfcs/2788-standard-lazy-types.html), which [is already implemented](https://doc.rust-lang.org/nightly/std/sync/struct.LazyLock.html), and [part of it is already stabilized](https://doc.rust-lang.org/nightly/std/sync/struct.OnceLock.html).

Thank you very much for this information.

> [@chrefr](#):
>
> Also note that there is no need to use `Arc` in `static`, and [`Mutex::new()`](https://doc.rust-lang.org/stable/std/sync/struct.Mutex.html#method.new) and [`VecDeque::new()`](https://doc.rust-lang.org/stable/std/collections/struct.VecDeque.html#method.new) are already `const`, so the second `static` can be a normal static:

you're absolutely right, I didn't pay any attention to this detail, the memory location is static, so Arc has no use here. Thanks again 🙏

---

<div class="post-metadata">

**Author:** ![theemathas](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/theemathas/32/11817_2.png) [@theemathas](https://internals.rust-lang.org/u/theemathas)\
**Post date:** [February 23, 2024, 5:57am UTC](https://internals.rust-lang.org/t/lazy-static-native/20372/5 "2024-02-23T05:57:22Z")

</div>

There's also the once\_cell crate that does something similar without using macros.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [May 23, 2024, 5:58am UTC](https://internals.rust-lang.org/t/lazy-static-native/20372/6 "2024-05-23T05:58:17Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
