From a1668451e1e6a81b6cd9569e11ab6f56e3c05e2c Mon Sep 17 00:00:00 2001 From: lukabavdaz Date: Wed, 31 Oct 2018 04:01:42 +0100 Subject: [PATCH 1/2] 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 892fd6d..32838d5 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 ac4b4b6..cc2cb25 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 0000000..33e589d --- /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; From 1de73921662c7f2ccd040b53e84f98ca86fb2656 Mon Sep 17 00:00:00 2001 From: lukabavdaz Date: Wed, 31 Oct 2018 21:49:25 +0100 Subject: [PATCH 2/2] Fix readme The link for ex6,rs should now work properly. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cc2cb25..3d5c742 100644 --- a/README.md +++ b/README.md @@ -192,7 +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) +- ["ex6.rs"](https://play.rust-lang.org/?code=%2F%2F+ex6.rs%0D%0A%2F%2F+Make+me+compile%21+Scroll+down+for+hints+%3A%29%0D%0A%0D%0Afn+main%28%29+%7B%0D%0A++++let+robot_name+%3D+Some%28String%3A%3Afrom%28%22Bors%22%29%29%3B%0D%0A%0D%0A++++match+robot_name+%7B%0D%0A++++++++Some%28name%29+%3D%3E+println%21%28%22Found+a+name%3A+%7B%7D%22%2C+name%29%2C%0D%0A++++++++None+%3D%3E+%28%29%2C%0D%0A++++%7D%0D%0A%0D%0A++++println%21%28%22robot_name+is%3A+%7B%3A%3F%7D%22%2C+robot_name%29%3B%0D%0A%7D%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%2F%2F+Hint%3A+The+following+two+statements+are+equivalent%3A%0D%0A%2F%2F+let+x+%3D+%26y%3B%0D%0A%2F%2F+let+ref+x+%3D+y%3B%0D%0A) ## To help with this repo/TODO list