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.74 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, b: u32) -> Box<dyn Future<Item = (), Error = ()> + Send> {
  21. println!("{}", b);
  22. Box::new(ok("Example").and_then(move |_| {
  23. lazy(move || {
  24. if b > 1 {
  25. tokio::spawn(self.rec(b - 1));
  26. }
  27. Ok(())
  28. })
  29. }))
  30. }
  31. }
  32.  
  33. fn main() {
  34. let my_struct = MyStruct::new();
  35. tokio::run(my_struct(10));
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement