Initial commit
This commit is contained in:
10
src/bin/lazylock.rs
Normal file
10
src/bin/lazylock.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use std::sync::LazyLock;
|
||||
|
||||
static GLOBAL_DATA: LazyLock<String> = LazyLock::new(|| {
|
||||
// Initialize the global data here
|
||||
"Hello, world!".to_string()
|
||||
});
|
||||
|
||||
fn main() {
|
||||
println!("{}", *GLOBAL_DATA); // Output: Hello, world!
|
||||
}
|
||||
15
src/bin/oncelock.rs
Normal file
15
src/bin/oncelock.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use std::sync::OnceLock;
|
||||
|
||||
static GLOBAL_DATA: OnceLock<String> = OnceLock::new();
|
||||
|
||||
fn get_global_data() -> &'static String {
|
||||
GLOBAL_DATA.get_or_init(|| {
|
||||
// Initialize the global data here
|
||||
"Hello, world!".to_string()
|
||||
})
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let data = get_global_data();
|
||||
println!("{}", data); // Output: Hello, world!
|
||||
}
|
||||
Reference in New Issue
Block a user