mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-26 00:00:03 +03:00
Add exercise for creating references in patterns
This commit is contained in:
parent
af8550f68c
commit
a1668451e1
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
47
ex6.rs
Normal file
47
ex6.rs
Normal file
|
@ -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;
|
Loading…
Reference in a new issue