From b644558c19dd1f0319204f50c1c162562edb79b1 Mon Sep 17 00:00:00 2001 From: mokou Date: Thu, 14 Jul 2022 17:34:50 +0200 Subject: fix: rename option to options --- exercises/option/README.md | 20 ----------- exercises/option/option1.rs | 23 ------------ exercises/option/option2.rs | 25 ------------- exercises/option/option3.rs | 19 ---------- exercises/options/README.md | 20 +++++++++++ exercises/options/options1.rs | 23 ++++++++++++ exercises/options/options2.rs | 25 +++++++++++++ exercises/options/options3.rs | 19 ++++++++++ info.toml | 84 +++++++++++++++++++++---------------------- 9 files changed, 129 insertions(+), 129 deletions(-) delete mode 100644 exercises/option/README.md delete mode 100644 exercises/option/option1.rs delete mode 100644 exercises/option/option2.rs delete mode 100644 exercises/option/option3.rs create mode 100644 exercises/options/README.md create mode 100644 exercises/options/options1.rs create mode 100644 exercises/options/options2.rs create mode 100644 exercises/options/options3.rs diff --git a/exercises/option/README.md b/exercises/option/README.md deleted file mode 100644 index 89c0028..0000000 --- a/exercises/option/README.md +++ /dev/null @@ -1,20 +0,0 @@ -# Option - -Type Option represents an optional value: every Option is either Some and contains a value, or None, and does not. -Option types are very common in Rust code, as they have a number of uses: -- Initial values -- Return values for functions that are not defined over their entire input range (partial functions) -- Return value for otherwise reporting simple errors, where None is returned on error -- Optional struct fields -- Struct fields that can be loaned or "taken" -- Optional function arguments -- Nullable pointers -- Swapping things out of difficult situations - -## Further Information - -- [Option Enum Format](https://doc.rust-lang.org/stable/book/ch10-01-syntax.html#in-enum-definitions) -- [Option Module Documentation](https://doc.rust-lang.org/std/option/) -- [Option Enum Documentation](https://doc.rust-lang.org/std/option/enum.Option.html) -- [if let](https://doc.rust-lang.org/rust-by-example/flow_control/if_let.html) -- [while let](https://doc.rust-lang.org/rust-by-example/flow_control/while_let.html) diff --git a/exercises/option/option1.rs b/exercises/option/option1.rs deleted file mode 100644 index 17cf4f6..0000000 --- a/exercises/option/option1.rs +++ /dev/null @@ -1,23 +0,0 @@ -// option1.rs -// Make me compile! Execute `rustlings hint option1` for hints - -// I AM NOT DONE - -// you can modify anything EXCEPT for this function's signature -fn print_number(maybe_number: Option) { - println!("printing: {}", maybe_number.unwrap()); -} - -fn main() { - print_number(13); - print_number(99); - - let mut numbers: [Option; 5]; - for iter in 0..5 { - let number_to_add: u16 = { - ((iter * 1235) + 2) / (4 * 16) - }; - - numbers[iter as usize] = number_to_add; - } -} diff --git a/exercises/option/option2.rs b/exercises/option/option2.rs deleted file mode 100644 index c6b83ec..0000000 --- a/exercises/option/option2.rs +++ /dev/null @@ -1,25 +0,0 @@ -// option2.rs -// Make me compile! Execute `rustlings hint option2` for hints - -// I AM NOT DONE - -fn main() { - let optional_word = Some(String::from("rustlings")); - // TODO: Make this an if let statement whose value is "Some" type - word = optional_word { - println!("The word is: {}", word); - } else { - println!("The optional word doesn't contain anything"); - } - - let mut optional_integers_vec: Vec> = Vec::new(); - for x in 1..10 { - optional_integers_vec.push(Some(x)); - } - - // TODO: make this a while let statement - remember that vector.pop also adds another layer of Option - // You can stack `Option`'s into while let and if let - integer = optional_integers_vec.pop() { - println!("current value: {}", integer); - } -} diff --git a/exercises/option/option3.rs b/exercises/option/option3.rs deleted file mode 100644 index 045d2ac..0000000 --- a/exercises/option/option3.rs +++ /dev/null @@ -1,19 +0,0 @@ -// option3.rs -// Make me compile! Execute `rustlings hint option3` for hints - -// I AM NOT DONE - -struct Point { - x: i32, - y: i32, -} - -fn main() { - let y: Option = Some(Point { x: 100, y: 200 }); - - match y { - Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y), - _ => println!("no match"), - } - y; // Fix without deleting this line. -} diff --git a/exercises/options/README.md b/exercises/options/README.md new file mode 100644 index 0000000..6140a16 --- /dev/null +++ b/exercises/options/README.md @@ -0,0 +1,20 @@ +# Options + +Type Option represents an optional value: every Option is either Some and contains a value, or None, and does not. +Option types are very common in Rust code, as they have a number of uses: +- Initial values +- Return values for functions that are not defined over their entire input range (partial functions) +- Return value for otherwise reporting simple errors, where None is returned on error +- Optional struct fields +- Struct fields that can be loaned or "taken" +- Optional function arguments +- Nullable pointers +- Swapping things out of difficult situations + +## Further Information + +- [Option Enum Format](https://doc.rust-lang.org/stable/book/ch10-01-syntax.html#in-enum-definitions) +- [Option Module Documentation](https://doc.rust-lang.org/std/option/) +- [Option Enum Documentation](https://doc.rust-lang.org/std/option/enum.Option.html) +- [if let](https://doc.rust-lang.org/rust-by-example/flow_control/if_let.html) +- [while let](https://doc.rust-lang.org/rust-by-example/flow_control/while_let.html) diff --git a/exercises/options/options1.rs b/exercises/options/options1.rs new file mode 100644 index 0000000..9d96817 --- /dev/null +++ b/exercises/options/options1.rs @@ -0,0 +1,23 @@ +// options1.rs +// Execute `rustlings hint options1` or use the `hint` watch subcommand for a hint. + +// I AM NOT DONE + +// you can modify anything EXCEPT for this function's signature +fn print_number(maybe_number: Option) { + println!("printing: {}", maybe_number.unwrap()); +} + +fn main() { + print_number(13); + print_number(99); + + let mut numbers: [Option; 5]; + for iter in 0..5 { + let number_to_add: u16 = { + ((iter * 1235) + 2) / (4 * 16) + }; + + numbers[iter as usize] = number_to_add; + } +} diff --git a/exercises/options/options2.rs b/exercises/options/options2.rs new file mode 100644 index 0000000..a1c21d3 --- /dev/null +++ b/exercises/options/options2.rs @@ -0,0 +1,25 @@ +// options2.rs +// Make me compile! Execute `rustlings hint option2` for hints + +// I AM NOT DONE + +fn main() { + let optional_word = Some(String::from("rustlings")); + // TODO: Make this an if let statement whose value is "Some" type + word = optional_word { + println!("The word is: {}", word); + } else { + println!("The optional word doesn't contain anything"); + } + + let mut optional_integers_vec: Vec> = Vec::new(); + for x in 1..10 { + optional_integers_vec.push(Some(x)); + } + + // TODO: make this a while let statement - remember that vector.pop also adds another layer of Option + // You can stack `Option`'s into while let and if let + integer = optional_integers_vec.pop() { + println!("current value: {}", integer); + } +} diff --git a/exercises/options/options3.rs b/exercises/options/options3.rs new file mode 100644 index 0000000..4cba4ad --- /dev/null +++ b/exercises/options/options3.rs @@ -0,0 +1,19 @@ +// options3.rs +// Make me compile! Execute `rustlings hint option3` for hints + +// I AM NOT DONE + +struct Point { + x: i32, + y: i32, +} + +fn main() { + let y: Option = Some(Point { x: 100, y: 200 }); + + match y { + Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y), + _ => println!("no match"), + } + y; // Fix without deleting this line. +} diff --git a/info.toml b/info.toml index 0a94f7d..6bcf278 100644 --- a/info.toml +++ b/info.toml @@ -531,6 +531,48 @@ path = "exercises/quiz2.rs" mode = "test" hint = "No hints this time ;)" +# OPTIONS + +[[exercises]] +name = "options1" +path = "exercises/options/options1.rs" +mode = "compile" +hint = """ +Hint 1: Check out some functions of Option: +is_some +is_none +unwrap + +and: +pattern matching + +Hint 2: There are no sensible defaults for the value of an Array; the values need to be filled before use. +""" + +[[exercises]] +name = "options2" +path = "exercises/options/options2.rs" +mode = "compile" +hint = """ +check out: +https://doc.rust-lang.org/rust-by-example/flow_control/if_let.html +https://doc.rust-lang.org/rust-by-example/flow_control/while_let.html + +Remember that Options can be stacked in if let and while let. +For example: Some(Some(variable)) = variable2 +Also see Option::flatten +""" + +[[exercises]] +name = "options3" +path = "exercises/options/options3.rs" +mode = "compile" +hint = """ +The compiler says a partial move happened in the `match` +statement. How can this be avoided? The compiler shows the correction +needed. After making the correction as suggested by the compiler, do +read: https://doc.rust-lang.org/std/keyword.ref.html""" + # ERROR HANDLING [[exercises]] @@ -661,48 +703,6 @@ ReportCard struct generic, but also the correct property - you will need to chan of the struct slightly too...you can do it! """ -# OPTIONS - -[[exercises]] -name = "option1" -path = "exercises/option/option1.rs" -mode = "compile" -hint = """ -Hint 1: Check out some functions of Option: -is_some -is_none -unwrap - -and: -pattern matching - -Hint 2: There are no sensible defaults for the value of an Array; the values need to be filled before use. -""" - -[[exercises]] -name = "option2" -path = "exercises/option/option2.rs" -mode = "compile" -hint = """ -check out: -https://doc.rust-lang.org/rust-by-example/flow_control/if_let.html -https://doc.rust-lang.org/rust-by-example/flow_control/while_let.html - -Remember that Options can be stacked in if let and while let. -For example: Some(Some(variable)) = variable2 -Also see Option::flatten -""" - -[[exercises]] -name = "option3" -path = "exercises/option/option3.rs" -mode = "compile" -hint = """ -The compiler says a partial move happened in the `match` -statement. How can this be avoided? The compiler shows the correction -needed. After making the correction as suggested by the compiler, do -read: https://doc.rust-lang.org/std/keyword.ref.html""" - # TRAITS [[exercises]] -- cgit v1.2.3