Questions tagged [lazy-static]

28 questions
12
votes
2 answers

Are there still reasons to use lazy_static?

lazy_static is a very popular crate. Years ago it had no better alternatives for certain tasks. But today, are there still any reasons to choose lazy_static over the newer once_cell or the upcoming LazyLock?
at54321
  • 8,726
  • 26
  • 46
6
votes
4 answers

alternative to using 'await' with lazy_static! macro in rust?

I want to use Async MongoDB in a project. I don't want to pass around the client because it would need to go around multiple tasks and threads. So I kept a static client using lazy_static. However, I can't use await in the initialization block. What…
zireael9797
  • 103
  • 2
  • 7
4
votes
2 answers

Return a reference to a T inside a lazy static RwLock>?

I have a lazy static struct that I want to be able to set to some random value in the beginning of the execution of the program, and then get later. This little silly snippet can be used as an example: use lazy_static::lazy_static; use…
Anders
  • 8,307
  • 9
  • 56
  • 88
3
votes
2 answers

How do I free memory in a lazy_static?

The documentation states that if the type has a destructor, it won't be called: https://docs.rs/lazy_static/1.4.0/lazy_static/#semantics So how am I supposed to free the memory?
gnevesdev
  • 47
  • 6
3
votes
1 answer

Define a static boolean in Rust from std::env::consts::OS=="windows"

I'd like to create a global static boolean value called IS_WINDOWS in a Rust file: lazy_static! { pub static ref IS_WINDOWS: bool = std::env::consts::OS=="windows"; } However, when I do this, anything that references the IS_WINDOWS value from…
LaVache
  • 2,372
  • 3
  • 24
  • 38
3
votes
1 answer

How to insert a String variable to a global mutable HashMap(using lazy_static and Mutex) without causing "does not live long enough" problem?

I am using Rust and I want to use a global mutable HashMap for convenience. However, while it is possible to define a global, mutable HashMap using lazy_static and Mutex, it is hard for my String variable defined in my function to have the same life…
3
votes
1 answer

The initialization function is called twice in a `lazy_static` block

I have a big project Where I use lazy_static to create a singleton. I think there is a bug in lazy_static crate (which appears in big projects only) or I am doing something wrong because the initialization function which must be called once to…
asmmo
  • 6,922
  • 1
  • 11
  • 25
3
votes
2 answers

How can return a reference to an item in a static hashmap inside a mutex?

I am trying to access a static hashmap for reading and writing but I am always getting error: use std::collections::HashMap; use std::sync::Mutex; pub struct ModuleItem { pub absolute_path: String, } lazy_static! { static ref MODULE_MAP:…
Alexandre
  • 3,088
  • 3
  • 34
  • 53
2
votes
1 answer

SQLx MySQL connection with lazy_static cannot be sent between threads safely

I use sqlx for initializing mysql connection (asynchronous) using lazy_static but weird errors occurred. This is the code that I wrote: use actix_web::middleware::Logger; use actix_web::web::{Data, JsonConfig}; use actix_web::{App,…
2
votes
1 answer

Rust lazy_static and tokio::sync::mpsc::channel in tokio::select

I started coding with rust lately and I'm loving it. I'm coding on a project where I want to "wrap" a C-API. In one case I have to define callbacks in Rust, which C can call. I let bindgen created the callbacks. Since the code need to run somewhat…
JenSeReal
  • 93
  • 10
2
votes
2 answers

Translating Cpp to Rust, handling a global static object

I'm a beginner translating a familiar Cpp project to Rust. The project contains a class called Globals which stores global config parameters. Here is an extract from its cpp file: static Globals &__get() { static Globals globals; return…
1
vote
1 answer

Rust: initialize a static variable/reference in a lib?

I'm new to Rust. I'm trying to create a static variable DATA of Vec in a library so that it is initialized after the compilation of the lib. I then include the lib in the main code hoping to use DATA directly without calling init_data() again.…
1
vote
1 answer

How to mutate static ref usize?

lazy_static! { static ref MY_GLOBAL: Mutex = Mutex::new(100); } MY_GLOBAL.lock().unwrap() += 1; This code gives me these errors: cannot use `+=` on type `MutexGuard<'_, usize>` cannot assign to this expression How do I mutate MY_GLOBAL?
GirkovArpa
  • 4,427
  • 4
  • 14
  • 43
1
vote
0 answers

Deferred initialization and mutable static borrowing [Rust]

My question is the following: Would it be possible to make a deferred initialization of an object and then borrow it as mutable for a 'static lifetime? Some context First of all, I am going to show an example of what I mean by deferred…
PauMAVA
  • 1,163
  • 14
  • 23
1
vote
1 answer

Filling a static array from different modules in Rust?

In my Rust project, I need a globally hold, static array or vec that is initialized once where modules can register values or functions on. I thought, this would be possible using the lazy_static!-crate, but it doesn't seem so. This is what I want…
glasflügel
  • 325
  • 2
  • 11
1
2