目に訴える1
lazy_static
はマクロを使っていますし、BACKTRACE
の本当の型がわかりません。
use lazy_static::lazy_static;
// `BACKTRACE` implements `Deref<Target = Option<String>>`
// and is initialized on the first access
lazy_static! {
static ref BACKTRACE: Option<String> = std::env::var("RUST_BACKTRACE").ok();
}
once_cell
は通常のRustの文法の範囲内ですし、BACKTRACE
の型も明示的に書かれています。
use std::sync::Lazy;
// `BACKTRACE` implements `Deref<Target = Option<String>>`
// and is initialized on the first access
static BACKTRACE: Lazy<Option<String>> = Lazy::new(|| {
std::env::var("RUST_BACKTRACE").ok()
});
権威に訴える
Nightlyの標準ライブラリには既に遅延初期化に関するAPIが入っていますがこれはonce_cellクレートからコピーされたものです。
The proposed API is directly copied from
once_cell
crate.
作者が訴える
once_cell
の作者がlazy_static
に取って代わるものだと言っています。
Add support for lazy initialized values to standard library, effectively superseding the popular
lazy_static
crate.
結論を訴える
once_cell
を使いましょう。