Compare commits
14 Commits
d3b4c0d653
...
my_solutio
| Author | SHA1 | Date | |
|---|---|---|---|
| 7a1ca00837 | |||
| 1789d43730 | |||
| 604a5a20ae | |||
| 0829174983 | |||
| ebe07acd1e | |||
| 4a4fc2ea0d | |||
| 91e6bf5ae4 | |||
| 3f57819e4c | |||
| d54a1e160b | |||
| 4913da035f | |||
| 1f0823a6a4 | |||
| 56075fa40e | |||
| 852cd5f0b9 | |||
| 1c16ac8c22 |
@@ -1,12 +1,12 @@
|
||||
mod ticket {
|
||||
struct Ticket {
|
||||
pub struct Ticket {
|
||||
title: String,
|
||||
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");
|
||||
}
|
||||
@@ -55,7 +55,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 +68,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(),
|
||||
// };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,17 @@ pub mod ticket {
|
||||
// - `title` that returns the `title` field.
|
||||
// - `description` that returns the `description` field.
|
||||
// - `status` that returns the `status` field.
|
||||
pub fn title(self) -> String {
|
||||
self.title
|
||||
}
|
||||
|
||||
pub fn description(self) -> String {
|
||||
self.description
|
||||
}
|
||||
|
||||
pub fn status(self) -> String {
|
||||
self.status
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,21 +11,9 @@ pub struct Ticket {
|
||||
|
||||
impl Ticket {
|
||||
pub fn new(title: String, description: String, status: String) -> Ticket {
|
||||
if title.is_empty() {
|
||||
panic!("Title cannot be empty");
|
||||
}
|
||||
if title.len() > 50 {
|
||||
panic!("Title cannot be longer than 50 bytes");
|
||||
}
|
||||
if description.is_empty() {
|
||||
panic!("Description cannot be empty");
|
||||
}
|
||||
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");
|
||||
}
|
||||
validate_title(&title);
|
||||
validate_description(&description);
|
||||
validate_status(&status);
|
||||
|
||||
Ticket {
|
||||
title,
|
||||
@@ -45,6 +33,45 @@ impl Ticket {
|
||||
pub fn status(&self) -> &String {
|
||||
&self.status
|
||||
}
|
||||
|
||||
pub fn set_title(&mut self, title: String) {
|
||||
validate_title(&title);
|
||||
self.title = title;
|
||||
}
|
||||
|
||||
pub fn set_description(&mut self, description: String) {
|
||||
validate_description(&description);
|
||||
self.description = description;
|
||||
}
|
||||
|
||||
pub fn set_status(&mut self, status: String) {
|
||||
validate_status(&status);
|
||||
self.status = status;
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_title(title: &String) {
|
||||
if title.is_empty() {
|
||||
panic!("Title cannot be empty");
|
||||
}
|
||||
if title.len() > 50 {
|
||||
panic!("Title cannot be longer than 50 bytes");
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_description(description: &String) {
|
||||
if description.is_empty() {
|
||||
panic!("Description cannot be empty");
|
||||
}
|
||||
if description.len() > 500 {
|
||||
panic!("Description cannot be longer than 500 bytes");
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_status(status: &String) {
|
||||
if status != "To-Do" && status != "In Progress" && status != "Done" {
|
||||
panic!("Only `To-Do`, `In Progress`, and `Done` statuses are allowed");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -6,16 +6,16 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn u16_size() {
|
||||
assert_eq!(size_of::<u16>(), todo!());
|
||||
assert_eq!(size_of::<u16>(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn i32_size() {
|
||||
assert_eq!(size_of::<i32>(), todo!());
|
||||
assert_eq!(size_of::<i32>(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bool_size() {
|
||||
assert_eq!(size_of::<bool>(), todo!());
|
||||
assert_eq!(size_of::<bool>(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn string_size() {
|
||||
assert_eq!(size_of::<String>(), todo!());
|
||||
assert_eq!(size_of::<String>(), 24);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -23,6 +23,6 @@ mod tests {
|
||||
// but, in general, the memory layout of structs is a more complex topic.
|
||||
// If you're curious, check out the "Data layout" section of the Rustonomicon
|
||||
// https://doc.rust-lang.org/nomicon/data.html for more information.
|
||||
assert_eq!(size_of::<Ticket>(), todo!());
|
||||
assert_eq!(size_of::<Ticket>(), 72);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,16 +13,16 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn u16_ref_size() {
|
||||
assert_eq!(size_of::<&u16>(), todo!());
|
||||
assert_eq!(size_of::<&u16>(), 8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn u64_mut_ref_size() {
|
||||
assert_eq!(size_of::<&mut u64>(), todo!());
|
||||
assert_eq!(size_of::<&mut u64>(), 8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ticket_ref_size() {
|
||||
assert_eq!(size_of::<&Ticket>(), todo!());
|
||||
assert_eq!(size_of::<&Ticket>(), 8);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// We'll pick the concept up again in a later chapter after covering traits and
|
||||
// interior mutability.
|
||||
fn outro() -> &'static str {
|
||||
"I have a basic understanding of __!"
|
||||
"I have a basic understanding of destructors!"
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -11,3 +11,74 @@
|
||||
// Integration here has a very specific meaning: they test **the public API** of your project.
|
||||
// You'll need to pay attention to the visibility of your types and methods; integration
|
||||
// tests can't access private or `pub(crate)` items.
|
||||
pub struct Order {
|
||||
product_name: String,
|
||||
quantity: u32,
|
||||
unit_price: u32,
|
||||
}
|
||||
|
||||
impl Order {
|
||||
pub fn new(product_name: String, quantity: u32, unit_price: u32) -> Order {
|
||||
validate_product_name(&product_name);
|
||||
validate_quantity(&quantity);
|
||||
validate_unit_price(&unit_price);
|
||||
|
||||
Order {
|
||||
product_name,
|
||||
quantity,
|
||||
unit_price,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn product_name(&self) -> &String {
|
||||
&self.product_name
|
||||
}
|
||||
|
||||
pub fn quantity(&self) -> &u32 {
|
||||
&self.quantity
|
||||
}
|
||||
|
||||
pub fn unit_price(&self) -> &u32 {
|
||||
&self.unit_price
|
||||
}
|
||||
|
||||
pub fn set_product_name(&mut self, product_name: String) {
|
||||
validate_product_name(&product_name);
|
||||
self.product_name = product_name;
|
||||
}
|
||||
|
||||
pub fn set_quantity(&mut self, quantity: u32) {
|
||||
validate_quantity(&quantity);
|
||||
self.quantity = quantity;
|
||||
}
|
||||
|
||||
pub fn set_unit_price(&mut self, unit_price: u32) {
|
||||
validate_unit_price(&unit_price);
|
||||
self.unit_price = unit_price;
|
||||
}
|
||||
|
||||
pub fn total(&self) -> u32 {
|
||||
self.quantity * self.unit_price
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_product_name(product_name: &String) {
|
||||
if product_name.is_empty() {
|
||||
panic!("Product name cannot be empty");
|
||||
}
|
||||
if product_name.len() > 300 {
|
||||
panic!("Product name cannot be longer than 300 bytes");
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_quantity(quantity: &u32) {
|
||||
if quantity == &0 {
|
||||
panic!("Quantity must be greater than zero");
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_unit_price(unit_price: &u32) {
|
||||
if unit_price == &0 {
|
||||
panic!("Unit price must be greater than zero");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
fn intro() -> &'static str {
|
||||
// TODO: fix me 👇
|
||||
"I'm ready to __!"
|
||||
"I'm ready to learn about traits!"
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -3,6 +3,22 @@
|
||||
//
|
||||
// Then implement the trait for `u32` and `i32`.
|
||||
|
||||
pub trait IsEven {
|
||||
fn is_even(self) -> bool;
|
||||
}
|
||||
|
||||
impl IsEven for u32 {
|
||||
fn is_even(self) -> bool {
|
||||
self % 2 == 0
|
||||
}
|
||||
}
|
||||
|
||||
impl IsEven for i32 {
|
||||
fn is_even(self) -> bool {
|
||||
self % 2 == 0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -3,9 +3,3 @@
|
||||
// a foreign type (`u32`, from `std`).
|
||||
// Look at the compiler error to get familiar with what it looks like.
|
||||
// Then delete the code below and move on to the next exercise.
|
||||
|
||||
impl PartialEq for u32 {
|
||||
fn eq(&self, _other: &Self) -> bool {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,13 @@ struct Ticket {
|
||||
|
||||
// TODO: Implement the `PartialEq` trait for `Ticket`.
|
||||
|
||||
impl PartialEq for Ticket {}
|
||||
impl PartialEq for Ticket {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.title == other.title
|
||||
&& self.description == other.description
|
||||
&& self.status == other.status
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
// print both sides of the comparison to the terminal.
|
||||
// If the compared type doesn't implement `Debug`, it doesn't know how to represent them!
|
||||
|
||||
#[derive(PartialEq)]
|
||||
#[derive(Debug, PartialEq)]
|
||||
struct Ticket {
|
||||
title: String,
|
||||
description: String,
|
||||
|
||||
@@ -6,7 +6,10 @@
|
||||
// collections (e.g. BTreeMap).
|
||||
|
||||
/// Return the minimum of two values.
|
||||
pub fn min<T>(left: T, right: T) -> T {
|
||||
pub fn min<T>(left: T, right: T) -> T
|
||||
where
|
||||
T: Ord,
|
||||
{
|
||||
if left <= right {
|
||||
left
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user