This crate provides a generic Lazy<T, Eval>
wrapper struct for lazy initialization.
It can be used for expensive-to-calculate T
values to ensure that the evaluation logic runs
only once and only if needed.
For example: ```rust use sloth::Lazy;
fn getexpensivestring() -> String { // do something expensive here to obtain the result, // such as read and process file contents
String::from("some expensive string we got from a file or something")
}
fn getexpensivei32() -> i32 { // do something expensive here to calculate the result, // such as build a supercomputer and wait 7.5 million years
42
}
let lazystring = Lazy::new(getexpensivestring); let lazyi32 = Lazy::new(getexpensivei32);
//...
let mustusestring = true;
//...
if mustusestring { println!("Expensive string is: {}", lazy_string.value_ref()); println!("It has length: {}", (lazystring.valueref()).len());
// get_expensive_string() has been called only once,
// get_expensive_i32() has not been called at all
} else { println!("Expensive int is: {}", lazyi32.value()); println!("It is{} divisible by 6", if lazyi32.value() % 6 == 0 { "" } else { " not" });
// get_expensive_string() has not been called,
// get_expensive_i32() has been called only once
}
```
Current version: 0.1.0