diff --git a/exercises/generic-type/src/lib.rs b/exercises/generic-type/src/lib.rs index 9015fd09..ea0dc233 100644 --- a/exercises/generic-type/src/lib.rs +++ b/exercises/generic-type/src/lib.rs @@ -1,6 +1,18 @@ // Exercise 1 // Implement struct Point to make it work. // Make it compile +struct Position { + x: T, + y: T, +} + +impl Position { + fn new(x: T, y: T) -> Position { + Position { x, y } + } +} + + fn exercise1() { let integer = Position { x: 5, y: 10 }; let float = Position { x: 1.0, y: 4.0 }; @@ -11,9 +23,9 @@ fn exercise1() { // Exercise 2 // Modify this struct to make the code work // Make it compile -struct Point { - x: T, - y: T, +struct Point { + x: T1, + y: T2, } fn exercise2() { @@ -26,12 +38,12 @@ fn exercise2() { // Exercise 3 // Make it compile // Add generic for Val to make the code work, DON'T modify the code in `main`. -struct Val { - val: f64, +struct Val { + val: T, } -impl Val { - fn value(&self) -> &f64 { +impl Val { + fn value(&self) -> &T { &self.val } } @@ -49,23 +61,28 @@ fn exercise3() { // Implementing logic // Run tests -fn find_max(collection: &[T]) -> Option<&T> { - todo!() +fn find_max(collection: &[T]) -> Option<&T> +{ + collection.iter().max() } // Exercise 5 // Reverse the elements in a collection // Make it compile // Run tests -fn reverse_collection(collection: &[T]) { - todo!() +fn reverse_collection(collection: &[T]) -> Vec +{ + let mut reversed = collection.to_vec(); + reversed.reverse(); + reversed } // Exercise 6 // Function to check if a collection contains a specific value -fn contains_value(collection: &[T], value: &T) -> bool { - todo!() +fn contains_value(collection: &[T], value: &T) -> bool +{ + collection.contains(value) } // Unit tests @@ -98,7 +115,7 @@ mod tests { #[test] fn test_reverse_collection_with_numbers() { let mut numbers = vec![1, 2, 3, 4, 5]; - reverse_collection(&mut numbers); + numbers = reverse_collection(&mut numbers); assert_eq!(numbers, vec![5, 4, 3, 2, 1]); } @@ -106,7 +123,7 @@ mod tests { #[test] fn test_reverse_collection_with_strings() { let mut strings = vec!["apple", "banana", "cherry", "durian"]; - reverse_collection(&mut strings); + strings = reverse_collection(&mut strings); assert_eq!(strings, vec!["durian", "cherry", "banana", "apple"]); } diff --git a/exercises/traits/src/lib.rs b/exercises/traits/src/lib.rs index 154cfb0d..43ca8175 100644 --- a/exercises/traits/src/lib.rs +++ b/exercises/traits/src/lib.rs @@ -13,10 +13,19 @@ trait Hello { //TODO struct Student {} impl Hello for Student { + fn say_something(&self) -> String { + String::from("I'm a good student") + } } //TODO struct Teacher {} impl Hello for Teacher { + fn say_hi(&self) -> String { + String::from("Hi, I'm your new teacher") + } + fn say_something(&self) -> String { + String::from("I'm not a bad teacher") + } } @@ -24,6 +33,7 @@ impl Hello for Teacher { // Make it compile in unit test for exercise 2 // Hint: use #[derive] for struct Point // Run tests +#[derive(Debug, PartialEq)] struct Point { x: i32, y: i32, @@ -35,10 +45,20 @@ struct Point { // Implement `fn sum` with trait bound in two ways. // Run tests // Hint: Trait Bound -fn sum(x: T, y: T) -> T { +use std::ops::Add; + +fn sum>(x: T, y: T) -> T { x + y } +trait Addable { + fn add(self, other: Self) -> Self; +} + +fn sum_other(x: T, y: T) -> T { + x.add(y) +} + // Exercise 4 // Fix errors and implement @@ -57,13 +77,13 @@ impl Foo for String { } // IMPLEMENT below with generics and parameters -fn static_dispatch(x) { - todo!() +fn static_dispatch(x: T) { + println!("{}", x.method()); } // Implement below with trait objects and parameters -fn dynamic_dispatch(x) { - todo!() +fn dynamic_dispatch(x: &dyn Foo) { + println!("{}", x.method()); } // Exercise 5 @@ -90,7 +110,7 @@ fn draw_with_box(x: Box) { x.draw(); } -fn draw_with_ref(x: __) { +fn draw_with_ref(x: &dyn Draw) { x.draw(); } @@ -106,12 +126,26 @@ trait Container { fn is_empty(&self) -> bool; } -struct Stack { - items: Vec, +struct Stack { + items: Vec, } //TODO implement Container for Stack +impl Container for Stack { + type Item = u8; + + fn insert(&mut self, item: Self::Item) { + self.items.push(item); + } + fn remove(&mut self) -> Option { + self.items.pop() + } + + fn is_empty(&self) -> bool { + self.items.is_empty() + } +} #[cfg(test)] @@ -161,7 +195,7 @@ mod tests { let y = 8u8; // Draw x. - draw_with_box(__); + draw_with_box(Box::new(x)); // Draw y. draw_with_ref(&y);