From 8034b71058a30061e1b0ce29cf7ea5f5ccd2178b Mon Sep 17 00:00:00 2001 From: Fabian Schmidt Date: Mon, 3 Feb 2025 11:43:07 +0100 Subject: [PATCH] Initial commit --- .gitignore | 1 + Cargo.lock | 7 +++++++ Cargo.toml | 6 ++++++ src/bin/lazylock.rs | 10 ++++++++++ src/bin/oncelock.rs | 15 +++++++++++++++ 5 files changed, 39 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/bin/lazylock.rs create mode 100644 src/bin/oncelock.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..f9e17f3 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "lazy_static_alt" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..0bdcc12 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "lazy_static_alt" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/bin/lazylock.rs b/src/bin/lazylock.rs new file mode 100644 index 0000000..962117c --- /dev/null +++ b/src/bin/lazylock.rs @@ -0,0 +1,10 @@ +use std::sync::LazyLock; + +static GLOBAL_DATA: LazyLock = LazyLock::new(|| { + // Initialize the global data here + "Hello, world!".to_string() +}); + +fn main() { + println!("{}", *GLOBAL_DATA); // Output: Hello, world! +} diff --git a/src/bin/oncelock.rs b/src/bin/oncelock.rs new file mode 100644 index 0000000..3139e60 --- /dev/null +++ b/src/bin/oncelock.rs @@ -0,0 +1,15 @@ +use std::sync::OnceLock; + +static GLOBAL_DATA: OnceLock = 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! +}