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

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

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.

Tuples

Coding Question 1: Tuple Manipulation

Task: Create a function that receives a tuple (i32, i32) and returns the sum and product of its elements.

Example Solution:

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
}

Coding Question 2: Extract Elements from a Tuple

Task: Implement a function that takes a tuple (i32, String, f64) and returns its elements in reverse order.

Example Solution:

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)
}

Closures

Coding Question 3: Closure Composition

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.

Example Solution:

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
}

Coding Question 4: Closure Counter

Task: Create a closure that acts as a counter, incrementing its internal count with each call.

Example Solution:

fn main() {
    let mut counter = 0;
    let mut increment_counter = || {
        counter += 1;
        counter
    };

    println!("{}", increment_counter()); // Output: 1
    println!("{}", increment_counter()); // Output: 2
}

Enums

Coding Question 5: Direction Enum

Task: Define an enum representing cardinal directions (North, South, East, West), and implement a function that returns the opposite direction.

Example Solution:

#[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
}

Coding Question 6: Enum Matching

Task: Write a function that takes an enum variant and performs different operations based on the variant.

Example Solution:

#[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
}

Practice Questions:

  1. Tuple Concatenation: Write a function that concatenates two tuples of the same type.

  2. Closure Accumulator: Implement a closure that accumulates and returns the sum of all passed values.

  3. 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://medium.com/techwasti

https://www.youtube.com/@maheshwarligade

https://www.techwasti.com/

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

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

Let’s connect on Stackoverflow, LinkedIn, & Twitter.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!