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
1 change: 0 additions & 1 deletion .gitignore

This file was deleted.

8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ members = [
"./exercises/basic-of-rust",
"./exercises/ownership-borrowing",
"./exercises/complex-type",
"./exercises/generic-type",
"./exercises/traits",

]

Expand All @@ -33,5 +35,13 @@ path = "./exercises/complex-type/src/structs.rs"
name = "enums"
path = "./exercises/complex-type/src/enums.rs"

[[test]]
name = "generic-type"
path = "./exercises/generic-type/src/lib.rs"

[[test]]
name = "traits"
path = "./exercises/traits/src/lib.rs"


[dependencies]
8 changes: 8 additions & 0 deletions exercises/generic-type/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "generic-type"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
4 changes: 4 additions & 0 deletions exercises/generic-type/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## Complete Generic type exercises
### Generic type

+ Make it compile, add logic code `Generic Type` exercises in `exercises/generic-type/src/lib.rs`
147 changes: 147 additions & 0 deletions exercises/generic-type/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Exercise 1
// Implement struct Point to make it work.
// Make it compile
fn exercise1() {
let integer = Position { x: 5, y: 10 };
let float = Position { x: 1.0, y: 4.0 };
}
struct Position<T> {
x: T,
y: T,
}


// Exercise 2
// Modify this struct to make the code work
// Make it compile
struct Point<T, U> {
x: T,
y: U,
}

fn exercise2() {
// DON'T modify this code.
let p = Point{x: 5, y : "hello".to_string()};
}



// Exercise 3
// Make it compile
// Add generic for Val to make the code work, DON'T modify the code in `main`.
struct Val<T>{
val: T,
}

impl<T>Val<T>{
fn value(&self) -> &T{
&self.val
}
}


fn exercise3() {
let x = Val{ val: 3.0 };
let y = Val{ val: "hello".to_string()};
println!("{}, {}", x.value(), y.value());
}

// Exercise 4
// Find the maximum value in a collection
// Make it compile
// Implementing logic
// Run tests

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: &mut [T]) {
collection.reverse();
}


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

// Unit tests
#[cfg(test)]
mod tests {
use super::*;

// Test for exercise 4
#[test]
fn test_find_max_with_numbers() {
let numbers = vec![1, 5, 3, 8, 2];
assert_eq!(find_max(&numbers), Some(&8));
}

// Test for exercise 4
#[test]
fn test_find_max_with_strings() {
let strings = vec!["apple", "banana", "cherry", "durian"];
assert_eq!(find_max(&strings), Some(&"durian"));
}

// Test for exercise 4
#[test]
fn test_find_max_with_empty_collection() {
let empty: Vec<i32> = Vec::new();
assert_eq!(find_max(&empty), None);
}

// Test for exercise 5
#[test]
fn test_reverse_collection_with_numbers() {
let mut numbers = vec![1, 2, 3, 4, 5];
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);
assert_eq!(strings, vec!["durian", "cherry", "banana", "apple"]);
}

// Test for exercise 5
#[test]
fn test_reverse_collection_with_empty_collection() {
let mut empty: Vec<i32> = Vec::new();
reverse_collection(&mut empty);
assert_eq!(empty, Vec::<i32>::new());
}

// Test for exercise 6
#[test]
fn test_contains_value_with_numbers() {
let numbers = vec![1, 2, 3, 4, 5];
assert_eq!(contains_value(&numbers, &3), true);
assert_eq!(contains_value(&numbers, &6), false);
}

// Test for exercise 6
#[test]
fn test_contains_value_with_strings() {
let strings = vec!["apple", "banana", "cherry", "durian"];
assert_eq!(contains_value(&strings, &"banana"), true);
assert_eq!(contains_value(&strings, &"grape"), false);
}

// Test for exercise 6
#[test]
fn test_contains_value_with_empty_collection() {
let empty: Vec<i32> = Vec::new();
assert_eq!(contains_value(&empty, &5), false);
}

}
8 changes: 8 additions & 0 deletions exercises/traits/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "traits"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
10 changes: 10 additions & 0 deletions exercises/traits/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## Complete Trait exercises
### Trait

+ Make it compile and Complete `Traits` exercises in `exercises/traits/src/lib.rs`

+ Run tests to check your implementation

```
cargo test --test traits
```
Loading