Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement