# Day 24: Practice coding exercise on String and collections in Rust.

Rust offers a plethora of features and data structures that are instrumental in solving various programming challenges. Let's dive into a set of coding exercises to sharpen your proficiency with strings, collections, and iterators in Rust.

## **String Manipulation**

### **Problem Statement 1: Reverse a String**

**Task:** Create a function that takes a string and returns its reverse.

#### Example:

```rust
fn reverse_string(s: &str) -> String {
    s.chars().rev().collect()
}

fn main() {
    let text = "Rust is amazing!";
    let reversed = reverse_string(text);
    println!("Reversed String: {}", reversed); // Output: "!gnizam si tsuR"
}
```

### **Problem Statement 2: Check for Palindrome**

**Task:** Implement a function to determine if a given string is a palindrome.

#### Example:

```rust
fn is_palindrome(s: &str) -> bool {
    let reversed = s.chars().rev().collect::<String>();
    s == reversed
}

fn main() {
    let text = "racecar";
    if is_palindrome(text) {
        println!("It's a palindrome!");
    } else {
        println!("It's not a palindrome.");
    }
}
```

## **Collection Operations**

### **Problem Statement 3: Find the Maximum Element**

**Task:** Create a function to find the maximum element in a vector.

#### Example:

```rust
fn find_max_element(arr: &[i32]) -> Option<i32> {
    arr.iter().cloned().max()
}

fn main() {
    let numbers = vec![4, 7, 2, 9, 5];
    if let Some(max) = find_max_element(&numbers) {
        println!("Maximum Element: {}", max); // Output: 9
    } else {
        println!("The vector is empty.");
    }
}
```

### **Problem Statement 4: Filter Even Numbers**

**Task:** Write a function to filter out even numbers from a vector.

#### Example:

```rust
fn filter_even_numbers(arr: &[i32]) -> Vec<i32> {
    arr.iter().cloned().filter(|&x| x % 2 == 0).collect()
}

fn main() {
    let numbers = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    let even_numbers = filter_even_numbers(&numbers);
    println!("Even Numbers: {:?}", even_numbers); // Output: [2, 4, 6, 8, 10]
}
```

## **Iterator Exploration**

### **Problem Statement 5: Sum of Squares**

**Task:** Compute the sum of squares for a given vector of integers.

#### Example:

```rust
fn sum_of_squares(arr: &[i32]) -> i32 {
    arr.iter().map(|&x| x * x).sum()
}

fn main() {
    let numbers = vec![1, 2, 3, 4, 5];
    let result = sum_of_squares(&numbers);
    println!("Sum of Squares: {}", result); // Output: 55
}
```

### **Problem Statement 6: Custom Iterator Function**

**Task:** Implement a custom iterator that generates a Fibonacci sequence.

#### Example:

```rust
struct Fibonacci {
    a: u32,
    b: u32,
}

impl Iterator for Fibonacci {
    type Item = u32;

    fn next(&mut self) -> Option<Self::Item> {
        let c = self.a + self.b;
        self.a = self.b;
        self.b = c;
        Some(c)
    }
}

fn main() {
    let fib = Fibonacci { a: 0, b: 1 };
    for val in fib.take(10) {
        print!("{} ", val); // Output: 1 2 3 5 8 13 21 34 55 89
    }
}
```

## **Practice Challenges:**

### **Challenge 1: Anagram Checker**

**Task:** Implement a function to determine if two strings are anagrams (contain the same characters but in a different order).

### **Challenge 2: Word Frequency Counter**

**Task:** Create a function that counts the frequency of each word in a given text and returns the word-frequency mapping.

### **Challenge 3: Implement Stack & Queue**

**Task:** Build a stack and a queue data structure from scratch using vectors or linked lists.

---

These exercises and challenges are designed to reinforce your understanding of strings, collections, iterators, and their usage in Rust. Practice them to strengthen your skills and become more proficient in solving problems using Rust's powerful features! Happy coding!

I hope this helps, you!!

**More such articles:**

[https://medium.com/techwasti](Link)

[https://www.youtube.com/@maheshwarligade](https://www.youtube.com/@maheshwarligade)

[https://www.techwasti.com/](Link)

\==========================\*\*=========================

**If this article adds any value to you then please clap and comment.**

**Let’s connect on** [**Stackoverflow**](http://stackoverflow.com/users/3187349/maheshwar-ligade)**,** [**LinkedIn**](https://in.linkedin.com/in/maheshwar-ligade-14447841)**, &** [**Twitter**](https://twitter.com/MaheshwarLigade)**.**
