diff options
author | Adam Carpenter <53hornet@gmail.com> | 2019-02-28 16:01:38 -0500 |
---|---|---|
committer | Adam Carpenter <53hornet@gmail.com> | 2019-02-28 16:01:38 -0500 |
commit | db0289b1813b73a93c22f12379ba501dbadc7552 (patch) | |
tree | 1ca4b3531745860afa55525532b7bd57220df66b /functional/src | |
parent | 3cee41f4686cf7cfe4e12c29fd222be89f811f14 (diff) | |
download | learning-rust-db0289b1813b73a93c22f12379ba501dbadc7552.tar.xz learning-rust-db0289b1813b73a93c22f12379ba501dbadc7552.zip |
Updated functional.
Diffstat (limited to 'functional/src')
-rw-r--r-- | functional/src/main.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/functional/src/main.rs b/functional/src/main.rs new file mode 100644 index 0000000..4fbc447 --- /dev/null +++ b/functional/src/main.rs @@ -0,0 +1,50 @@ +use std::thread; +use std::time::Duration; + +struct Cacher<T> + where T: Fn(u32) -> u32 +{ + calculation: T, + value: Option<u32>, +} + +fn main() { + let simulated_user_specified_value = 10; + let simulated_random_number = 7; + + generate_workout( + simulated_user_specified_value, + simulated_random_number + ); +} + +fn generate_workout(intensity: u32, random_number: u32) { + let expensive_closure = |num| { + println!("calculating slowly..."); + thread::sleep(Duration::from_secs(2)); + num + }; + + if intensity < 25 { + println!( + "Today, do {} pushups!", + expensive_closure(intensity) + ); + println!( + "Next, do {} situps!", + expensive_closure(intensity) + ); + } + else { + if random_number == 3 { + println!("Take a break today! Remember to stay hydrated!"); + } + else { + println!( + "Today, run for {} minutes!", + expensive_closure(intensity) + ); + } + } +} + |