| Crates.io | lazy-into |
| lib.rs | lazy-into |
| version | 0.2.0 |
| created_at | 2025-10-11 20:31:30.350007+00 |
| updated_at | 2025-10-12 18:38:32.638903+00 |
| description | Lazy conversion from one type to another |
| homepage | |
| repository | https://github.com/jsode64/lazy-into |
| max_upload_size | |
| id | 1878530 |
| size | 11,743 |
Lazy conversion from one type to another in Rust.
Converts from types T to U with a mapping function that is only called the first time it's needed.
This is useful when you have an expensive conversion that may not even be needed.
For example, if you have a CLI app that parses deep into JSON data that the user can see by typing "data", a LazyInto could be useful.
If the user never says "data", the search is not needed.
If the user says "data" multiple times, the search is only needed once.
A regular LazyCell can not take input parameters.
fn main() {
use lazy_into::LazyInto;
let a = LazyInto::new(42, |n| n as f64 * 2.0);
let b = LazyInto::new(21, TryInto::try_into);
let mut c = LazyInto::new(-1, |n| n.to_string());
assert_eq!(a.get(), &84.0);
assert_eq!(*a, 84.0);
assert_eq!(*b, Ok(21u32));
assert_eq!(*c, "-1");
c.get_mut().push_str("2345");
assert_eq!(*c, "-12345");
}