An example of hand-written coroutines 151 5. At this point, we change the state to State::Wait1 and we store the future we want to resolve so we can access it in the next state. 6. Our state machine has now changed its state from Start to Wait1. Since we're looping on the match statement, we immediately progress to the next state and will reach the match arm in State::Wait1 on the next iteration. 7. The first thing we do in Wait1 to call poll on the Future instance we're waiting on. 8. If the future returns Poll State::Not Ready, we simply bubble that up to the caller by breaking out of the loop and returning Not Ready. 9. If the future returns Poll State::Ready together with our data, we know that we can execute the instructions that rely on the data from the first future and advance to the next state. In our case, we only print out the returned data, so that's only one line of code. 10. Next, we get to the point where we get a new future by calling Http::get. We set the state to Wait2, just like we did when going from State::Start to State::Wait1. 11. Like we did the first time we got a future that we needed to resolve before we continue, we save it so we can access it in State::Wait2. 12. Since we're in a loop, the next thing that happens is that we reach the matching arm for Wait2, and here, we repeat the same steps as we did for State::Wait1 but on a different future. 13. If it returns Ready with our data, we act on it and we set the final state of our Coroutine to State::Resolved. There is one more important change: this time, we want to communicate to the caller that this future is done, so we break out of the loop and return Poll State::Ready. If anyone tries to call poll on our Coroutine again, we will panic, so the caller must make sure to keep track of when the future returns Poll State::Ready and make sure to not call poll on it ever again. The last thing we do before we get to our main function is create a new Coroutine in a function we call async_main. This way, we can keep the changes to a minimum when we start talking about async/await in the last part of this chapter: ch07/a-coroutine/src/main. rs fn async_main()-> impl Future<Output = ()> { Coroutine::new() } So, at this point, we're finished writing our coroutine and the only thing left is to write some logic to drive our state machine through its different stages of the main function.