Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate futures; // 0.1.25
- extern crate tokio; // 0.1.14
- use futures::future::lazy;
- use futures::future::*;
- use std::result::Result::Ok;
- use tokio::prelude::Future;
- struct MyStruct {
- a: String,
- }
- impl MyStruct {
- fn new() -> Self {
- Self {
- a: "Hello".to_string(),
- }
- }
- // recursive function
- fn rec(&self, c: u32) -> Box<dyn Future<Item = (), Error = ()> + Send> {
- println!("{}", self.a);
- println!("{}", c);
- Box::new(ok("Example").and_then(move |_| {
- lazy(move || {
- println!("{}", self.a);
- if c > 1 {
- tokio::spawn(self.rec(c - 1));
- }
- Ok(())
- })
- }))
- }
- }
- fn main() {
- let my_struct = MyStruct::new();
- tokio::run(my_struct(10));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement