SHARE
TWEET

Untitled

a guest Sep 16th, 2019 112 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. trait Lazy<T> {
  2.     fn force(self) -> T;
  3. }
  4.  
  5. struct LazyVal<T>(pub T);
  6. fn lazy<T>(val: T) -> LazyVal<T> {
  7.     LazyVal(val)
  8. }
  9.  
  10. impl<T, S: FnOnce() -> T> Lazy<T> for S {
  11.   fn force(self) -> T {
  12.       self()
  13.   }
  14. }
  15.  
  16. impl<T> Lazy<T> for LazyVal<T> {
  17.     fn force(self) -> T {
  18.         self.0
  19.     }
  20. }
  21.  
  22. fn get<T, V: Lazy<T>>(val: V) -> T {
  23.   val.force()
  24. }
  25.  
  26. fn main() {
  27.     let val = || 5;
  28.     let forced = get(val);
  29.     let reforced = get(lazy(forced));
  30.     println!("Hello {}, {}", forced, reforced);
  31. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top