signal: Ctrl+C example: highlight power of Stream::for_each()

This commit is contained in:
Jules Kerssemakers
2018-09-10 11:30:00 -07:00
committed by Carl Lerche
parent da47cfbd58
commit 3db92496f6
+9 -4
View File
@@ -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!();