mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-16 00:00:12 +02:00
signal: Ctrl+C example: highlight power of Stream::for_each()
This commit is contained in:
committed by
Carl Lerche
parent
da47cfbd58
commit
3db92496f6
@@ -16,13 +16,18 @@ fn main() {
|
||||
|
||||
println!("This program is now waiting for you to press Ctrl+C");
|
||||
|
||||
// for_each is a powerful primitive provided by the Futures crate
|
||||
// it turns a Stream into a Future that completes after all stream-items
|
||||
// have been completed.
|
||||
let future = stream.for_each(|()| {
|
||||
println!("Ctrl+C received!");
|
||||
Ok(())
|
||||
});
|
||||
|
||||
// Up until now, we haven't really DONE anything, just prepared
|
||||
// now it's time to actually schedule, and thus execute, the stream
|
||||
// on our event loop
|
||||
core.run(stream.for_each(|()| {
|
||||
println!("Ctrl-C received!");
|
||||
Ok(())
|
||||
})).unwrap();
|
||||
core.run(future).unwrap();
|
||||
|
||||
println!("this won't be printed, because the received Ctrl+C will also kill the program");
|
||||
unreachable!();
|
||||
|
||||
Reference in New Issue
Block a user