Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. struct Foo {
  2. add: u32,
  3. }
  4.  
  5. impl Foo {
  6. fn make_work<M, W>(&self, make_work: M) -> impl Fn(u32) -> u32 + 'static
  7. where
  8. M: FnOnce() -> W,
  9. W: Fn(u32) -> u32 + 'static,
  10. {
  11. let work = make_work();
  12. let add = self.add;
  13. move |item| work(item) + add
  14. }
  15. }
  16.  
  17. fn main() {
  18. #[derive(Clone)]
  19. struct MoveOnly(u32);
  20.  
  21. let (work, _moved_out) = {
  22. let add = MoveOnly(5);
  23. let foo = Foo { add: 10 };
  24. let work = {
  25. let add = add.clone();
  26. foo.make_work(move || {
  27. move |item| add.0 + item
  28. })
  29. };
  30.  
  31. (work, add)
  32. };
  33.  
  34. println!("{}", work(100));
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement