mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-06 00:00:10 +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");
|
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
|
// Up until now, we haven't really DONE anything, just prepared
|
||||||
// now it's time to actually schedule, and thus execute, the stream
|
// now it's time to actually schedule, and thus execute, the stream
|
||||||
// on our event loop
|
// on our event loop
|
||||||
core.run(stream.for_each(|()| {
|
core.run(future).unwrap();
|
||||||
println!("Ctrl-C received!");
|
|
||||||
Ok(())
|
|
||||||
})).unwrap();
|
|
||||||
|
|
||||||
println!("this won't be printed, because the received Ctrl+C will also kill the program");
|
println!("this won't be printed, because the received Ctrl+C will also kill the program");
|
||||||
unreachable!();
|
unreachable!();
|
||||||
|
|||||||
Reference in New Issue
Block a user