Guest User

Untitled

a guest
Apr 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. #![feature(generic_associated_types)]
  2.  
  3. trait MyFn {
  4. type Output<'a>; // <-- we added <'a> to make it generic
  5. fn call(&self) -> Self::Output;
  6. }
  7.  
  8. impl<T> MyFn for Baz<T> {
  9. type Output<'a> = &'a T;
  10. fn call(&self) -> Self::Output {
  11. &self.0
  12. }
  13. }
  14.  
  15. struct Baz<T>(T);
  16.  
  17. fn make_baz<T>(t: T) -> Box<for<'a> MyFn<Output<'a> = &'a T>> {
  18. Box::new(Baz(t))
  19. }
  20.  
  21.  
  22. fn main() {
  23. let outside = {
  24. let s = "hi".to_string();
  25. let baz = make_baz(&s); // <-- NOW BORROWED!
  26. println!("{}", baz.call()); // works
  27.  
  28. baz
  29. };
  30.  
  31. println!("{}", outside.call()); // doesn't work!
  32. }
Add Comment
Please, Sign In to add comment