feat: added a closure exercise to demonstrate using irrefutable patterns in closure signatures

This commit is contained in:
jayber 2022-06-04 18:34:12 +01:00
parent d36d7b32e5
commit 12156a017b
3 changed files with 35 additions and 0 deletions

View File

@ -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);
});
}

View File

@ -4,3 +4,4 @@ mod closures3;
mod closures4; mod closures4;
mod closures5; mod closures5;
mod closures6; mod closures6;
mod closures7;

View File

@ -194,6 +194,14 @@ hint = """
There are two ways to solve this, neither is difficult and both involve just removing 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.""" 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 # IF
[[exercises]] [[exercises]]