Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A small macro for defining lazy evaluated static variables in Rust.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
NotificationsYou must be signed in to change notification settings

rust-lang-nursery/lazy-static.rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

A macro for declaring lazily evaluated statics in Rust.

Using this macro, it is possible to havestatics that require code to beexecuted at runtime in order to be initialized.This includes anything requiring heap allocations, like vectors or hash maps,as well as anything that requires non-const function calls to be computed.

RustLatest versionDocumentationLicense

Minimum supportedrustc

1.40.0+

This version is explicitly tested in CI and may only be bumped in new minor versions. Any changes to the supported minimum version will be called out in the release notes.

Getting Started

lazy-static.rs is available on crates.io.It is recommended to look there for the newest released version, as well as links to the newest builds of the docs.

At the point of the last update of this README, the latest published version could be used like this:

Add the following dependency to your Cargo manifest...

[dependencies]lazy_static ="1.5.0"

...and see thedocs for how to use it.

Example

use lazy_static::lazy_static;use std::collections::HashMap;lazy_static!{static refHASHMAP:HashMap<u32,&'staticstr> ={letmut m =HashMap::new();        m.insert(0,"foo");        m.insert(1,"bar");        m.insert(2,"baz");        m};}fnmain(){// First access to `HASHMAP` initializes itprintln!("The entry for `0` is\"{}\".",HASHMAP.get(&0).unwrap());// Any further access to `HASHMAP` just returns the computed valueprintln!("The entry for `1` is\"{}\".",HASHMAP.get(&1).unwrap());}

Standard library

It is now possible to easily replicate this crate's functionality in Rust's standard library withstd::sync::LazyLock. The example above could also be written as:

use std::collections::HashMap;use std::sync::LazyLock;staticHASHMAP:LazyLock<HashMap<u32,&str>> =LazyLock::new(||{letmut m =HashMap::new();    m.insert(0,"foo");    m.insert(1,"bar");    m.insert(2,"baz");    m});fnmain(){// First access to `HASHMAP` initializes itprintln!("The entry for `0` is\"{}\".",HASHMAP.get(&0).unwrap());// Any further access to `HASHMAP` just returns the computed valueprintln!("The entry for `1` is\"{}\".",HASHMAP.get(&1).unwrap());}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submittedfor inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without anyadditional terms or conditions.

About

A small macro for defining lazy evaluated static variables in Rust.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors56

Languages


[8]ページ先頭

©2009-2025 Movatter.jp