#lazy-evaluation #static #macro

无 std lazy_fn

为函数提供 lazy_static 功能!

3 个稳定版本

1.0.2 2023 年 3 月 27 日
1.0.1 2022 年 4 月 27 日

#30#lazy

Download history 23/week @ 2024-04-24 7/week @ 2024-05-08 45/week @ 2024-05-15 11/week @ 2024-05-22 5/week @ 2024-05-29 12/week @ 2024-06-05 9/week @ 2024-06-12 7/week @ 2024-06-19 5/week @ 2024-06-26 17/week @ 2024-07-03 2/week @ 2024-07-17 32/week @ 2024-07-24

56 每月下载量

MIT/Apache

7KB

crates.io docs.rs license

lazy_fn

lazy_static 为函数提供支持!

使属性函数 "lazy",它只会被评估一次,并将结果缓存起来,从而作为静态引用返回。

用法

在您的 Cargo.toml

[dependencies]
lazy_fn = "1"
lazy_static = "1"

在您的代码中

#[macro_use] extern crate lazy_fn;

#[lazy_fn]
fn maths() -> i32 {
    9 + 10
}

#[lazy_fn]
fn hello_world() -> &'static str {
    "hello, world!"
}

#[lazy_fn]
fn hello_fmt() -> String {
    format!("hello, {}!", "world")
}

let maths: &'static i32 = maths();
let hello_world: &'static str = hello_world();
let hello_fmt: &'static String = hello_fmt();

lib.rs:

lazy_static 为函数提供支持!

用法

在您的 Cargo.toml

[dependencies]
lazy_fn = "1"
lazy_static = "1"
#[lazy_fn]
fn maths() -> i32 {
    9 + 10
}

#[lazy_fn]
fn hello_world() -> &'static str {
    "hello, world!"
}

#[lazy_fn]
fn hello_fmt() -> String {
    format!("hello, {}!", "world")
}

let maths: &'static i32 = maths();
let hello_world: &'static str = hello_world();
let hello_fmt: &'static String = hello_fmt();

依赖

~1.5MB
~36K SLoC