Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait Lazy<T> {
- fn force(self) -> T;
- }
- struct LazyVal<T>(pub T);
- fn lazy<T>(val: T) -> LazyVal<T> {
- LazyVal(val)
- }
- impl<T, S: FnOnce() -> T> Lazy<T> for S {
- fn force(self) -> T {
- self()
- }
- }
- impl<T> Lazy<T> for LazyVal<T> {
- fn force(self) -> T {
- self.0
- }
- }
- fn get<T, V: Lazy<T>>(val: V) -> T {
- val.force()
- }
- fn main() {
- let val = || 5;
- let forced = get(val);
- let reforced = get(lazy(forced));
- println!("Hello {}, {}", forced, reforced);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement