Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

my completions #6

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": [
"struct"
]
}
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion exercises/03_ticket_v1/00_intro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
15 changes: 15 additions & 0 deletions exercises/03_ticket_v1/01_struct/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
27 changes: 23 additions & 4 deletions exercises/03_ticket_v1/02_validation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions exercises/03_ticket_v1/03_modules/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
20 changes: 9 additions & 11 deletions exercises/03_ticket_v1/04_visibility/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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");
}
Expand All @@ -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
Expand All @@ -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() {
Expand All @@ -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(),
// };
}
}
12 changes: 12 additions & 0 deletions exercises/03_ticket_v1/05_encapsulation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down
12 changes: 6 additions & 6 deletions exercises/03_ticket_v1/06_ownership/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down