Skip to content
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
47 changes: 32 additions & 15 deletions exercises/generic-type/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
// Exercise 1
// Implement struct Point to make it work.
// Make it compile
struct Position<T> {
x: T,
y: T,
}

impl<T> Position<T> {
fn new(x: T, y: T) -> Position<T> {
Position { x, y }
}
}


fn exercise1() {
let integer = Position { x: 5, y: 10 };
let float = Position { x: 1.0, y: 4.0 };
Expand All @@ -11,9 +23,9 @@ fn exercise1() {
// Exercise 2
// Modify this struct to make the code work
// Make it compile
struct Point<T> {
x: T,
y: T,
struct Point<T1, T2> {
x: T1,
y: T2,
}

fn exercise2() {
Expand All @@ -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 <T> {
val: T,
}

impl Val {
fn value(&self) -> &f64 {
impl <T> Val <T> {
fn value(&self) -> &T {
&self.val
}
}
Expand All @@ -49,23 +61,28 @@ fn exercise3() {
// Implementing logic
// Run tests

fn find_max<T>(collection: &[T]) -> Option<&T> {
todo!()
fn find_max<T: Ord>(collection: &[T]) -> Option<&T>
{
collection.iter().max()
}

// Exercise 5
// Reverse the elements in a collection
// Make it compile
// Run tests
fn reverse_collection<T>(collection: &[T]) {
todo!()
fn reverse_collection<T: Clone>(collection: &[T]) -> Vec<T>
{
let mut reversed = collection.to_vec();
reversed.reverse();
reversed
}


// Exercise 6
// Function to check if a collection contains a specific value
fn contains_value<T>(collection: &[T], value: &T) -> bool {
todo!()
fn contains_value<T: PartialEq>(collection: &[T], value: &T) -> bool
{
collection.contains(value)
}

// Unit tests
Expand Down Expand Up @@ -98,15 +115,15 @@ 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]);
}

// Test for exercise 5
#[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"]);
}

Expand Down
52 changes: 43 additions & 9 deletions exercises/traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,27 @@ 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")
}
}


// Exercise 2
// 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,
Expand All @@ -35,10 +45,20 @@ struct Point {
// Implement `fn sum` with trait bound in two ways.
// Run tests
// Hint: Trait Bound
fn sum<T>(x: T, y: T) -> T {
use std::ops::Add;

fn sum<T: Add<Output = T>>(x: T, y: T) -> T {
x + y
}

trait Addable {
fn add(self, other: Self) -> Self;
}

fn sum_other<T: Addable>(x: T, y: T) -> T {
x.add(y)
}


// Exercise 4
// Fix errors and implement
Expand All @@ -57,13 +77,13 @@ impl Foo for String {
}

// IMPLEMENT below with generics and parameters
fn static_dispatch(x) {
todo!()
fn static_dispatch<T: Foo>(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
Expand All @@ -90,7 +110,7 @@ fn draw_with_box(x: Box<dyn Draw>) {
x.draw();
}

fn draw_with_ref(x: __) {
fn draw_with_ref(x: &dyn Draw) {
x.draw();
}

Expand All @@ -106,12 +126,26 @@ trait Container {
fn is_empty(&self) -> bool;
}

struct Stack {
items: Vec<u8>,
struct Stack<T> {
items: Vec<T>,
}

//TODO implement Container for Stack
impl Container for Stack<u8> {
type Item = u8;

fn insert(&mut self, item: Self::Item) {
self.items.push(item);
}

fn remove(&mut self) -> Option<Self::Item> {
self.items.pop()
}

fn is_empty(&self) -> bool {
self.items.is_empty()
}
}


#[cfg(test)]
Expand Down Expand Up @@ -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);
Expand Down