mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-10 12:49:18 +00:00
feat: added a closure exercise to demonstrate using irrefutable patterns in closure signatures
This commit is contained in:
parent
d36d7b32e5
commit
12156a017b
26
exercises/closures/closures7.rs
Normal file
26
exercises/closures/closures7.rs
Normal 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);
|
||||
});
|
||||
}
|
||||
@ -4,3 +4,4 @@ mod closures3;
|
||||
mod closures4;
|
||||
mod closures5;
|
||||
mod closures6;
|
||||
mod closures7;
|
||||
|
||||
@ -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]]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user