From 3db92496f6d192b44713281a43fe91c93cd4fba1 Mon Sep 17 00:00:00 2001 From: Jules Kerssemakers Date: Wed, 7 Jun 2017 21:23:10 +0200 Subject: [PATCH] signal: Ctrl+C example: highlight power of Stream::for_each() --- examples/ctrl-c/src/main.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/examples/ctrl-c/src/main.rs b/examples/ctrl-c/src/main.rs index be8962215..16edd592a 100644 --- a/examples/ctrl-c/src/main.rs +++ b/examples/ctrl-c/src/main.rs @@ -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!();