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

Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
Search for a command to run...

Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
No comments yet. Be the first to comment.
Move beyond traditional RESTful thinking. Learn how to design APIs specifically for MCP (Model Context Protocol) servers. This guide covers the shift in mindset, a practical OpenAPI 3.1 example, and a Spring Boot implementation to make your services ...

Extending Kestra to Every Corner of Your Data Stack. Introduction: The Power of Plugins Imagine you're a master chef. You don't just have one knife - you have specialized tools for every task: a paring knife for delicate work, a chef's knife for chop...
Mastering Complex Orchestration Scenarios. Introduction: The Orchestrator's Toolkit Imagine you're conducting a symphony. You don't just wave your baton - you cue sections, adjust tempo, handle surprises, and ensure harmony. That's what advanced work...
From Data Extraction to Loading - A Practical Guide Introduction: Why ETL Still Matters in the Modern Data Stack Remember when data engineering was "extract, transform, load"? Some say ETL is dead, replaced by ELT, reverse ETL, and data mesh. But her...
Building Blocks of Declarative Orchestration. Introduction: The Power of Simplicity Imagine trying to build a house without understanding bricks, beams, and blueprints. That's what using an orchestration tool without understanding its core concepts f...
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.
Task: Create a function that takes a string and returns its reverse.
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"
}
Task: Implement a function to determine if a given string is a palindrome.
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.");
}
}
Task: Create a function to find the maximum element in a vector.
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.");
}
}
Task: Write a function to filter out even numbers from a vector.
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]
}
Task: Compute the sum of squares for a given vector of integers.
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
}
Task: Implement a custom iterator that generates a Fibonacci sequence.
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
}
}
Task: Implement a function to determine if two strings are anagrams (contain the same characters but in a different order).
Task: Create a function that counts the frequency of each word in a given text and returns the word-frequency mapping.
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://www.youtube.com/@maheshwarligade
\==========================**=========================
If this article adds any value to you then please clap and comment.
Let’s connect on Stackoverflow, LinkedIn, & Twitter.