Day 25:Mastering Rust: Coding Questions on Tuples, Closures, and Enums.

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, with its robust features and powerful constructs, empowers developers to solve intricate problems efficiently. Let's dive into some coding questions revolving around tuples, closures, and enums, crucial aspects of Rust programming, followed by their solutions. Additionally, we'll provide practice questions to solidify your understanding.
Task: Create a function that receives a tuple (i32, i32) and returns the sum and product of its elements.
fn sum_and_product(tup: (i32, i32)) -> (i32, i32) {
let (a, b) = tup;
(a + b, a * b)
}
fn main() {
let input = (3, 5);
let result = sum_and_product(input);
println!("Sum: {}, Product: {}", result.0, result.1); // Output: Sum: 8, Product: 15
}
Task: Implement a function that takes a tuple (i32, String, f64) and returns its elements in reverse order.
fn reverse_tuple(tup: (i32, String, f64)) -> (f64, String, i32) {
let (a, b, c) = tup;
(c, b, a)
}
fn main() {
let input = (42, String::from("Rust"), 3.14);
let result = reverse_tuple(input);
println!("{:?}", result); // Output: (3.14, "Rust", 42)
}
Task: Write a closure that takes an i32 input and returns its square, then create another closure that squares the result of the first closure.
fn main() {
let square = |x| x * x;
let square_result = square(5);
let square_square = |y| square(y) * square(y);
let square_square_result = square_square(5);
println!("Square: {}, Square of Square: {}", square_result, square_square_result);
// Output: Square: 25, Square of Square: 625
}
Task: Create a closure that acts as a counter, incrementing its internal count with each call.
fn main() {
let mut counter = 0;
let mut increment_counter = || {
counter += 1;
counter
};
println!("{}", increment_counter()); // Output: 1
println!("{}", increment_counter()); // Output: 2
}
Task: Define an enum representing cardinal directions (North, South, East, West), and implement a function that returns the opposite direction.
#[derive(Debug)]
enum Direction {
North,
South,
East,
West,
}
fn opposite_direction(dir: Direction) -> Direction {
match dir {
Direction::North => Direction::South,
Direction::South => Direction::North,
Direction::East => Direction::West,
Direction::West => Direction::East,
}
}
fn main() {
let current = Direction::North;
let opposite = opposite_direction(current);
println!("Opposite Direction: {:?}", opposite); // Output: Opposite Direction: South
}
Task: Write a function that takes an enum variant and performs different operations based on the variant.
#[derive(Debug)]
enum MathOperation {
Add(i32, i32),
Subtract(i32, i32),
Multiply(i32, i32),
}
fn perform_operation(op: MathOperation) -> i32 {
match op {
MathOperation::Add(a, b) => a + b,
MathOperation::Subtract(a, b) => a - b,
MathOperation::Multiply(a, b) => a * b,
}
}
fn main() {
let add = MathOperation::Add(10, 5);
let subtract = MathOperation::Subtract(10, 5);
let multiply = MathOperation::Multiply(10, 5);
println!("Addition: {}", perform_operation(add)); // Output: Addition: 15
println!("Subtraction: {}", perform_operation(subtract)); // Output: Subtraction: 5
println!("Multiplication: {}", perform_operation(multiply)); // Output: Multiplication: 50
}
Tuple Concatenation: Write a function that concatenates two tuples of the same type.
Closure Accumulator: Implement a closure that accumulates and returns the sum of all passed values.
Enum and Struct: Create an enum representing shapes (Circle, Square, Rectangle) and define a struct for each shape containing necessary properties.
These coding questions, solutions, and practice problems on tuples, closures, and enums are designed to enhance your Rust programming skills. Use these exercises as a starting point to delve deeper into Rust's advanced features and further solidify your understanding. 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.