1. async fn cancel_task() -> tokio::io::Result<()> {
    2. let handle = tokio::spawn(async {
    3. for i in 0..10 {
    4. tokio::time::sleep(std::time::Duration::from_secs(1)).await;
    5. println!("Ping! {}", i);
    6. }
    7. });
    8. tokio::time::sleep(std::time::Duration::from_secs(5)).await;
    9. handle.abort();
    10. match handle.await {
    11. Ok(_) => Ok(()),
    12. Err(e) if e.is_cancelled() => Ok(()),
    13. Err(e) => Err(e),
    14. }?;
    15. Ok(())
    16. }