This library provides a convenient derive macro for a once function.
fn_once = "0.3.0"
use fn_once::once;
#[once]
fn print_once(name: &str) {
println!("Hello! {}!", name);
}
#[once(or = { number })] // or = { block expression }
fn add_one_once(number: u32) -> u32 {
count + 1
}
#[once(panic)]
fn panic_when_twice() { }
fn main() {
print_once("World"); // print "Hello! World!"
print_once("foo"); // no effect
let mut number = 0;
number = add_one_once(number); // 0 + 1 -> 1
number = add_one_once(number); // no effect -> 1
number = add_one_once(number); // no effect -> 1
assert_eq!(number, 1);
panic_when_twice();
panic_when_twice(); // panic!
}
[features]
default = ["once_off"]
once_off = []
# The point is that
# just `once_off` feature is turned on.