diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000000..e903b8279e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "cSpell.words": [ + "struct" + ] +} \ No newline at end of file diff --git a/README.md b/README.md index 59969ed558..d9827f15c4 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,24 @@ the [`solutions` branch](https://github.com/mainmatter/100-exercises-to-learn-ru - [X] 00_intro - [X] 01_integers - [X] 02_variable +- [X] 03_if_else +- [X] 04_panics +- [X] 05_factorial +- [X] 06_while +- [X] 07_for +- [X] 08_overflow +- [X] 09_saturating +- [X] 10_as_casting + +### 03_ticket_v1 + +- [X] 00_intro +- [X] 01_struct +- [X] 02_validation +- [X] 03_modules +- [X] 04_visibility +- [X] 05_encapsulation +- [X] 06_ownership # License diff --git a/exercises/03_ticket_v1/00_intro/src/lib.rs b/exercises/03_ticket_v1/00_intro/src/lib.rs index f7afa4701e..c52e3ac9c2 100644 --- a/exercises/03_ticket_v1/00_intro/src/lib.rs +++ b/exercises/03_ticket_v1/00_intro/src/lib.rs @@ -1,6 +1,6 @@ fn intro() -> &'static str { // TODO: fix me 👇 - "I'm ready to __!" + "I'm ready to start modelling a software ticket!" } #[cfg(test)] diff --git a/exercises/03_ticket_v1/01_struct/src/lib.rs b/exercises/03_ticket_v1/01_struct/src/lib.rs index 1119e330aa..0b6310fc49 100644 --- a/exercises/03_ticket_v1/01_struct/src/lib.rs +++ b/exercises/03_ticket_v1/01_struct/src/lib.rs @@ -5,6 +5,21 @@ // It should also have a method named `is_available` that returns a `true` if the quantity is // greater than 0, otherwise `false`. +struct Order { + price: u32, + quantity: u32, +} + +impl Order { + fn is_available(self) -> bool { + if self.quantity > 0 { + true + } else { + false + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/03_ticket_v1/02_validation/src/lib.rs b/exercises/03_ticket_v1/02_validation/src/lib.rs index d7416eba11..745bbb44db 100644 --- a/exercises/03_ticket_v1/02_validation/src/lib.rs +++ b/exercises/03_ticket_v1/02_validation/src/lib.rs @@ -8,16 +8,35 @@ impl Ticket { // TODO: implement the `new` function. // The following requirements should be met: // - Only `To-Do`, `In Progress`, and `Done` statuses are allowed. - // - The `title` and `description` fields should not be empty. - // - the `title` should be at most 50 bytes long. - // - the `description` should be at most 500 bytes long. + // DONE - The `title` and `description` fields should not be empty. + // DONE - the `title` should be at most 50 bytes long. + // DONE - the `description` should be at most 500 bytes long. // The method should panic if any of the requirements are not met. // // You'll have to use what you learned in the previous exercises, // as well as some `String` methods. Use the documentation of Rust's standard library // to find the most appropriate options -> https://doc.rust-lang.org/std/string/struct.String.html fn new(title: String, description: String, status: String) -> Self { - todo!(); + if title.is_empty() { + panic!("Title cannot be empty"); + } + + if description.is_empty() { + panic!("Description cannot be empty"); + } + + if title.len() > 50 { + panic!("Title cannot be longer than 50 bytes") + } + + if description.len() > 500 { + panic!("Description cannot be longer than 500 bytes") + } + + if status != "To-Do" && status != "In Progress" && status != "Done" { + panic!("Only `To-Do`, `In Progress`, and `Done` statuses are allowed"); + } + Self { title, description, diff --git a/exercises/03_ticket_v1/03_modules/src/lib.rs b/exercises/03_ticket_v1/03_modules/src/lib.rs index df0762094b..1758233f09 100644 --- a/exercises/03_ticket_v1/03_modules/src/lib.rs +++ b/exercises/03_ticket_v1/03_modules/src/lib.rs @@ -2,6 +2,8 @@ mod helpers { // TODO: Make this code compile, either by adding a `use` statement or by using // the appropriate path to refer to the `Ticket` struct. + use super::Ticket; + fn create_todo_ticket(title: String, description: String) -> Ticket { Ticket::new(title, description, "To-Do".into()) } diff --git a/exercises/03_ticket_v1/04_visibility/src/lib.rs b/exercises/03_ticket_v1/04_visibility/src/lib.rs index b494fc945b..ba257bd699 100644 --- a/exercises/03_ticket_v1/04_visibility/src/lib.rs +++ b/exercises/03_ticket_v1/04_visibility/src/lib.rs @@ -1,12 +1,12 @@ mod ticket { - struct Ticket { + pub struct Ticket { title: String, - description: String, + pub description: String, status: String, } impl Ticket { - fn new(title: String, description: String, status: String) -> Ticket { + pub fn new(title: String, description: String, status: String) -> Ticket { if title.is_empty() { panic!("Title cannot be empty"); } @@ -32,8 +32,6 @@ mod ticket { } } -// TODO: **Exceptionally**, you'll be modifying both the `ticket` module and the `tests` module -// in this exercise. #[cfg(test)] mod tests { // TODO: Add the necessary `pub` modifiers in the parent module to remove the compiler @@ -55,7 +53,7 @@ mod tests { // // TODO: Once you have verified that the below does not compile, // comment the line out to move on to the next exercise! - assert_eq!(ticket.description, "A description"); + // assert_eq!(ticket.description, "A description"); } fn encapsulation_cannot_be_violated() { @@ -68,10 +66,10 @@ mod tests { // // TODO: Once you have verified that the below does not compile, // comment the lines out to move on to the next exercise! - let ticket = Ticket { - title: "A title".into(), - description: "A description".into(), - status: "To-Do".into(), - }; + // let ticket = Ticket { + // title: "A title".into(), + // description: "A description".into(), + // status: "To-Do".into(), + // }; } } diff --git a/exercises/03_ticket_v1/05_encapsulation/src/lib.rs b/exercises/03_ticket_v1/05_encapsulation/src/lib.rs index 91e06eb2ab..e46530fe84 100644 --- a/exercises/03_ticket_v1/05_encapsulation/src/lib.rs +++ b/exercises/03_ticket_v1/05_encapsulation/src/lib.rs @@ -31,9 +31,21 @@ pub mod ticket { } // TODO: Add three public methods to the `Ticket` struct: + // - `title` that returns the `title` field. + pub fn title(self) -> String { + self.title + } + // - `description` that returns the `description` field. + pub fn description(self) -> String { + self.description + } + // - `status` that returns the `status` field. + pub fn status(self) -> String { + self.status + } } } diff --git a/exercises/03_ticket_v1/06_ownership/src/lib.rs b/exercises/03_ticket_v1/06_ownership/src/lib.rs index ded2ad8de6..4ab49f1857 100644 --- a/exercises/03_ticket_v1/06_ownership/src/lib.rs +++ b/exercises/03_ticket_v1/06_ownership/src/lib.rs @@ -34,16 +34,16 @@ impl Ticket { } } - pub fn title(self) -> String { - self.title + pub fn title(&self) -> &String { + &self.title } - pub fn description(self) -> String { - self.description + pub fn description(&self) -> &String { + &self.description } - pub fn status(self) -> String { - self.status + pub fn status(&self) -> &String { + &self.status } }