Created
May 18, 2020 12:50
-
-
Save Code-Hex/f0407539c45c000285016dd065a5b29e to your computer and use it in GitHub Desktop.
rust 入門
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use futures::future::{Abortable, AbortHandle, Aborted}; | |
| use futures::executor::block_on; | |
| fn main() { | |
| println!("Hello, world!"); | |
| block_on(run()); | |
| } | |
| async fn run() { | |
| let (abort_handle, abort_registration) = AbortHandle::new_pair(); | |
| let future = Abortable::new(long_task(0), abort_registration); | |
| abort_handle.abort(); | |
| assert_eq!(future.await, Err(Aborted)); | |
| } | |
| async fn long_task(task_id: usize) { | |
| println!("Task {} on {:?}", task_id, std::thread::current().id()); | |
| std::thread::sleep(std::time::Duration::from_secs(1)); | |
| println!("Task {} done", task_id); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://qiita.com/kbone/items/7f7847376fac78b0ebcb