From a1668451e1e6a81b6cd9569e11ab6f56e3c05e2c Mon Sep 17 00:00:00 2001 From: lukabavdaz Date: Wed, 31 Oct 2018 04:01:42 +0100 Subject: [PATCH] Add exercise for creating references in patterns --- README-template.hbs | 1 + README.md | 1 + ex6.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 ex6.rs diff --git a/README-template.hbs b/README-template.hbs index 892fd6d1..32838d5b 100644 --- a/README-template.hbs +++ b/README-template.hbs @@ -179,6 +179,7 @@ A few exercises based on things I've encountered or had trouble with getting use {{ playground_link "ex3.rs" }} {{ playground_link "ex4.rs" }} {{ playground_link "ex5.rs" }} +{{ playground_link "ex6.rs" }} ## To help with this repo/TODO list diff --git a/README.md b/README.md index ac4b4b63..cc2cb253 100644 --- a/README.md +++ b/README.md @@ -192,6 +192,7 @@ A few exercises based on things I've encountered or had trouble with getting use - ["ex3.rs"](https://play.rust-lang.org/?code=%2F%2F+ex3.rs%0A%2F%2F+Make+me+compile%21%0A%0Astruct+Foo+%7B%0A++++capacity%3A+i32%2C%0A%7D%0A%0Afn+main%28%29+%7B%0A++++println%21%28%22%7B%3A%3F%7D%22%2C+Foo+%7B+capacity%3A+3+%7D%29%3B%0A%7D%0A) - ["ex4.rs"](https://play.rust-lang.org/?code=%2F%2F+ex4.rs%0A%2F%2F+Make+me+compile%21%0A%0Afn+something%28%29+-%3E+Result%3Ci32%2C+std%3A%3Anum%3A%3AParseIntError%3E+%7B%0A++++let+x%3Ai32+%3D+%223%22.parse%28%29%3B%0A++++Ok%28x+*+4%29%0A%7D%0A%0Afn+main%28%29+%7B%0A++++match+something%28%29+%7B%0A++++++++Ok%28..%29+%3D%3E+println%21%28%22You+win%21%22%29%2C%0A++++++++Err%28e%29+%3D%3E+println%21%28%22Oh+no+something+went+wrong%3A+%7B%7D%22%2C+e%29%2C%0A++++%7D%0A%7D%0A) - ["ex5.rs"](https://play.rust-lang.org/?code=%2F%2F+ex5.rs%0A%2F%2F+Make+me+compile%21%0A%0Aenum+Reaction%3C%27a%3E+%7B%0A++++Sad%28%26%27a+str%29%2C%0A++++Happy%28%26%27a+str%29%2C%0A%7D%0A%0Afn+express%28sentiment%3A+Reaction%29+%7B%0A++++match+sentiment+%7B%0A++++++++Reaction%3A%3ASad%28s%29+%3D%3E+println%21%28%22%3A%28+%7B%7D%22%2C+s%29%2C%0A++++++++Reaction%3A%3AHappy%28s%29+%3D%3E+println%21%28%22%3A%29+%7B%7D%22%2C+s%29%2C%0A++++%7D%0A%7D%0A%0Afn+main+%28%29+%7B%0A++++let+x+%3D+Reaction%3A%3AHappy%28%22It%27s+a+great+day+for+Rust%21%22%29%3B%0A++++express%28x%29%3B%0A++++express%28x%29%3B%0A++++let+y+%3D+Reaction%3A%3ASad%28%22This+code+doesn%27t+compile+yet.%22%29%3B%0A++++express%28y%29%3B%0A%7D%0A) +- ["ex6.rs"](https://play.rust-lang.org/?code=%2F%2F%20ex6.rs%0A%2F%2F%20Make%20me%20compile!%20Scroll%20down%20for%20hints%20%3A)%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20robot_name%20%3D%20Some(String%3A%3Afrom(%22Bors%22))%3B%0A%20%20%20%20%0A%20%20%20%20match%20robot_name%20%7B%0A%20%20%20%20%20%20%20%20Some(name)%20%3D%3E%20println!(%22Found%20a%20name%3A%20%7B%7D%22%2C%20name)%2C%0A%20%20%20%20%20%20%20%20None%20%3D%3E%20()%2C%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20println!(%22robot_name%20is%3A%20%7B%3A%3F%7D%22%2C%20robot_name)%3B%0A%7D%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F%20Hint%3A%20The%20following%20two%20statements%20are%20equivalent%3A%0A%2F%2F%20let%20x%20%3D%20%26y%3B%0A%2F%2F%20let%20ref%20x%20%3D%20y%3B) ## To help with this repo/TODO list diff --git a/ex6.rs b/ex6.rs new file mode 100644 index 00000000..33e589d4 --- /dev/null +++ b/ex6.rs @@ -0,0 +1,47 @@ +// ex6.rs +// Make me compile! Scroll down for hints :) + +fn main() { + let robot_name = Some(String::from("Bors")); + + match robot_name { + Some(name) => println!("Found a name: {}", name), + None => (), + } + + println!("robot_name is: {:?}", robot_name); +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +// Hint: The following two statements are equivalent: +// let x = &y; +// let ref x = y;