Questions tagged [lazy-static]

28 questions
1
vote
0 answers

Accessing disjoint entries in global HashMap for lifetime of thread in Rust

my current project requires recording some information for various events that happen during the execution of a thread. These events are saved in a global struct index by the thread id: RECORDER1: HashMap> =…
gatoWololo
  • 296
  • 4
  • 10
1
vote
1 answer

What is better in Rust: defining a static variable to access it globally or passing a non-static variable through function arguments?

If a variable is used in multiple functions in a file, is it better to define it as static rather than passing a non-static variable through function arguments? Does this have a performance impact? An example using a static variable: lazy_static! { …
1
vote
2 answers

Rust lazy static custom struct instance

In Rust, I am trying to declare a static instance of a custom struct. Because by default I am not able to assign other values than const ones, I am trying to use lazy_static. Here is my custom struct: pub struct MyStruct { field1: String, …
Adrien Chapelet
  • 386
  • 4
  • 24
0
votes
0 answers

error[E0432]: unresolved import `lazy_static`

i have this code : which gives me error when trying to : cargo build The error: cargo build Compiling front_end v0.1.0 (C:\dev\my\rust\workspace\front_end) error[E0432]: unresolved import `lazy_static` --> src\main.rs:4:5 | 4 | use…
user63898
  • 29,839
  • 85
  • 272
  • 514
0
votes
1 answer

Why does this return statement cause an error but the return expression doesn't in Rust?

I'm confused why this code works below: lazy_static! { static ref TSS: TaskStateSegment = { let mut tss = TaskStateSegment::new(); tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX as usize] = { const…
0
votes
0 answers

Stateful embedded library in Rust

I want to develop a library in Rust for microcontrollers that holds some state information. This state data cannot be transferred to the caller of the library. I am using #![no_std]. The library should be suitable for bare metal applications and…
Stefan
  • 83
  • 8
0
votes
2 answers

no rules expected this token in macro call during initialization of static variable using lazy_static crate

I am trying to initialize a static variable at runtime using lazy_static crate. But I'm getting no rules expected the token E1 error while compiling. This is the link lazy_static I followed use lazy_static::lazy_static; lazy_static! { static…
Harry
  • 2,177
  • 1
  • 19
  • 33
0
votes
1 answer

The item's lifetime of global HashMap by lazy_static in Rust

I am new to Rust. I define a global HashMap of User by lazy_static. There is a lifetime in User, so I have to set a lifetime in lazy_static. It seems that only 'static can be used in lazy_static. Here is the question: can I insert "non-static" User…
Bingzheng Wu
  • 435
  • 3
  • 11
0
votes
1 answer

Get item's reference from global HashMap in Rust

I'm trying to use a static HashMap to store some data that I want to use and modify globally in the future. I found out that some way of declaring such a global map is by using lazy_static and mutex in order to share data safely.…
Hèctor M.C.
  • 95
  • 1
  • 6
0
votes
0 answers

Rust: Why is lazy_static not working with some types

I need to create some global mutable variables, using lazy_static! with some types works, but with some other types it doesn't. The mutex is to change value. This works: lazy_static! { static ref foo: Mutex:…
Dee
  • 7,455
  • 6
  • 36
  • 70
0
votes
1 answer

What type should I use to return a lazy_static value?

I plan to have a struct which provides the JSON schema via a trait method. The schema is stored compiled in a lazy_static variable, but which type does my schema() function have to return? lazy_static::lazy_static! { static ref DEF:…
macalloy
  • 43
  • 7
0
votes
1 answer

Rust lazy_static variable RwLock access

I am trying to declare and read/write an instance of a custom struct, using lazy_static as I had to use non-const function at its initialization (string). As I saw here in an other Stackoverflow post, I tried to use RwLock, which works fine when it…
Adrien Chapelet
  • 386
  • 4
  • 24
-1
votes
1 answer

How to preload lazy_static variables in Rust?

I use Rust's lazy_static crate to assign a frequently used database object to a global variable but I do not want lazy loading. Is there a way to trigger lazy_static to preload the variable or is there a better way to achieve this? All the functions…
Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118
1
2