Skip to content

Instantly share code, notes, and snippets.

@Code-Hex
Created May 18, 2020 12:50
Show Gist options
  • Select an option

  • Save Code-Hex/f0407539c45c000285016dd065a5b29e to your computer and use it in GitHub Desktop.

Select an option

Save Code-Hex/f0407539c45c000285016dd065a5b29e to your computer and use it in GitHub Desktop.
rust 入門
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);
}
@Code-Hex
Copy link
Copy Markdown
Author

@Code-Hex
Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment