Skip to content

Commit 86b5c08

Browse files
sanjaykdragonfmoko
andauthored
feat: Add Option2 exercise (#290)
* added option2 * changed up the exercise, modified the help section * Update exercises/option/option2.rs Co-Authored-By: fmoko <mokou@posteo.net> * Update exercises/option/option2.rs Co-Authored-By: fmoko <mokou@posteo.net> * Update exercises/option/option2.rs Co-Authored-By: fmoko <mokou@posteo.net> Co-authored-by: fmoko <mokou@posteo.net>
1 parent 71d3125 commit 86b5c08

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

exercises/option/option2.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// option2.rs
2+
// Make me compile! Execute `rustlings hint option2` for hints
3+
4+
// I AM NOT DONE
5+
6+
fn main() {
7+
let optional_value = Some(String::from("rustlings"));
8+
// Make this an if let statement whose value is "Some" type
9+
value = optional_value {
10+
println!("the value of optional value is: {}", value);
11+
} else {
12+
println!("The optional value doesn't contain anything!");
13+
}
14+
15+
let mut optional_values_vec: Vec<Option<i8>> = Vec::new();
16+
for x in 1..10 {
17+
optional_values_vec.push(Some(x));
18+
}
19+
20+
// make this a while let statement - remember that vector.pop also adds another layer of Option<T>
21+
// You can stack `Option<T>`'s into while let and if let
22+
value = optional_values_vec.pop() {
23+
println!("current value: {}", value);
24+
}
25+
}

info.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,21 @@ and:
534534
pattern matching
535535
"""
536536

537+
[[exercises]]
538+
name = "option2"
539+
path = "exercises/option/option2.rs"
540+
mode = "compile"
541+
hint = """
542+
check out:
543+
https://doc.rust-lang.org/rust-by-example/flow_control/if_let.html
544+
https://doc.rust-lang.org/rust-by-example/flow_control/while_let.html
545+
546+
Remember that Options can be stacked in if let and while let.
547+
For example: Some(Some(variable)) = variable2
548+
549+
550+
"""
551+
537552
[[exercises]]
538553
name = "result1"
539554
path = "exercises/error_handling/result1.rs"

0 commit comments

Comments
 (0)