From 12156a017be68c6f136b7fb65e8175fa2c081a3b Mon Sep 17 00:00:00 2001 From: jayber Date: Sat, 4 Jun 2022 18:34:12 +0100 Subject: [PATCH] feat: added a closure exercise to demonstrate using irrefutable patterns in closure signatures --- exercises/closures/closures7.rs | 26 ++++++++++++++++++++++++++ exercises/closures/mod.rs | 1 + info.toml | 8 ++++++++ 3 files changed, 35 insertions(+) create mode 100644 exercises/closures/closures7.rs diff --git a/exercises/closures/closures7.rs b/exercises/closures/closures7.rs new file mode 100644 index 00000000..555aa39c --- /dev/null +++ b/exercises/closures/closures7.rs @@ -0,0 +1,26 @@ +// closure7.rs +// You can use patterns in closure (and function) parameters. +// However there is one big difference between the patterns +// you can use in these sorts of places (like let statements) +// and conditional places (like match). It's called Irrefutability. +// Make me compile! + +// Execute `rustlings hint closures7` for hints! + +// I AM NOT DONE + +fn main() { + + let clo = |(definitely, _)| { + println!("maybe {:?}", definitely); + }; + + clo(("oh yes", "oh no")); + + let maybe = Some("yes"); + let list = vec![maybe]; + + list.iter().inspect(|&Some(value)| { + println!("maybe {:?}", value); + }); +} \ No newline at end of file diff --git a/exercises/closures/mod.rs b/exercises/closures/mod.rs index 1a759063..53b75634 100644 --- a/exercises/closures/mod.rs +++ b/exercises/closures/mod.rs @@ -4,3 +4,4 @@ mod closures3; mod closures4; mod closures5; mod closures6; +mod closures7; diff --git a/info.toml b/info.toml index 40f9ee1a..6ea0defc 100644 --- a/info.toml +++ b/info.toml @@ -194,6 +194,14 @@ hint = """ There are two ways to solve this, neither is difficult and both involve just removing the offending part. If you're thinking hard, you've missed the point.""" +[[exercises]] +name = "closures7" +path = "exercises/closures/closures7.rs" +mode = "compile" +hint = """ +This is where the info is https://doc.rust-lang.org/book/ch18-02-refutability.html +So there isn't a satisfying solution, just an acceptance of the facts.""" + # IF [[exercises]]