Day 19: Array Coding Questions Rust Part-1

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...
Let us practice some coding exercises for arrays in Rust lang. Like some common questions like a reverse array, find the maximum number so that we will get familiar with the array.
fn find_max(arr: &[i32]) -> Option<i32> {
if arr.is_empty() {
return None;
}
let mut max = arr[0];
for &num in arr {
if num > max {
max = num;
}
}
Some(max)
}
fn main() {
let numbers = [4, 7, 2, 9, 5];
if let Some(max) = find_max(&numbers) {
println!("The maximum element is: {}", max);
} else {
println!("The array is empty.");
}
}
fn calculate_sum(arr: &[i32]) -> i32 {
let mut sum = 0;
for &num in arr {
sum += num;
}
sum
}
fn main() {
let numbers = [4, 7, 2, 9, 5];
let sum = calculate_sum(&numbers);
println!("The sum of elements is: {}", sum);
}
fn reverse_array(arr: &mut [i32]) {
let mut left = 0;
let mut right = arr.len() - 1;
while left < right {
arr.swap(left, right);
left += 1;
right -= 1;
}
}
fn main() {
let mut numbers = [4, 7, 2, 9, 5];
reverse_array(&mut numbers);
println!("Reversed array: {:?}", numbers);
}
fn contains_element(arr: &[i32], target: i32) -> bool {
for &num in arr {
if num == target {
return true;
}
}
false
}
fn main() {
let numbers = [4, 7, 2, 9, 5];
let target = 7;
if contains_element(&numbers, target) {
println!("The array contains {}.", target);
} else {
println!("The array does not contain {}.", target);
}
}
These sample solutions illustrate basic operations on arrays in Rust, including finding the maximum element, calculating the sum, reversing the array, and checking for a specific element. They showcase common Rust syntax and array manipulation techniques.
fn merge_sorted_arrays(arr1: &[i32], arr2: &[i32]) -> Vec<i32> {
let mut merged = Vec::new();
let (mut i, mut j) = (0, 0);
while i < arr1.len() && j < arr2.len() {
if arr1[i] < arr2[j] {
merged.push(arr1[i]);
i += 1;
} else {
merged.push(arr2[j]);
j += 1;
}
}
merged.extend_from_slice(&arr1[i..]);
merged.extend_from_slice(&arr2[j..]);
merged
}
fn main() {
let arr1 = [1, 3, 5, 7, 9];
let arr2 = [2, 4, 6, 8, 10];
let merged = merge_sorted_arrays(&arr1, &arr2);
println!("Merged and sorted array: {:?}", merged);
}
fn rotate_left(arr: &mut [i32], k: usize) {
let n = arr.len();
let k = k % n;
arr.rotate_left(k);
}
fn main() {
let mut numbers = [1, 2, 3, 4, 5];
let k = 3; // Rotate by 3 positions to the left
rotate_left(&mut numbers, k);
println!("Rotated array: {:?}", numbers);
}
fn find_second_largest(arr: &[i32]) -> Option<i32> {
if arr.len() < 2 {
return None;
}
let mut max = arr[0];
let mut second_max = i32::min_value();
for &num in arr.iter().skip(1) {
if num > max {
second_max = max;
max = num;
} else if num > second_max && num < max {
second_max = num;
}
}
Some(second_max)
}
fn main() {
let numbers = [7, 2, 9, 3, 11, 5];
if let Some(second_largest) = find_second_largest(&numbers) {
println!("Second largest element: {}", second_largest);
} else {
println!("Array too small.");
}
}
Mastering array operations in Rust is fundamental for handling data efficiently. Understanding how to iterate through arrays, perform basic computations, and manipulate array elements forms the backbone of many algorithms and data processing tasks. Rust's concise syntax and array handling capabilities facilitate the development of robust and performant code.
Happy coding with Rust!
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.