Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. extern crate futures; // 0.1.25
  2. extern crate tokio; // 0.1.14
  3.  
  4. use futures::future::lazy;
  5. use futures::future::*;
  6. use std::result::Result::Ok;
  7. use tokio::prelude::Future;
  8.  
  9. struct MyStruct {
  10. a: String,
  11. }
  12.  
  13. impl MyStruct {
  14. fn new() -> Self {
  15. Self {
  16. a: "Hello".to_string(),
  17. }
  18. }
  19. // recursive function
  20. fn rec(&self, c: u32) -> Box<dyn Future<Item = (), Error = ()> + Send> {
  21. // I would like to have mutable copies of self.a here
  22. println!("{}", self.a);
  23. println!("{}", c);
  24. Box::new(ok("Example").and_then(move |_| {
  25. lazy(move || {
  26. // I would like to have mutable copies of self.a here
  27. println!("{}", self.a);
  28. if c > 1 {
  29. tokio::spawn(self.rec(c - 1));
  30. }
  31. Ok(())
  32. })
  33. }))
  34. }
  35. }
  36.  
  37. fn main() {
  38. let my_struct = MyStruct::new();
  39. tokio::run(my_struct(10));
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement