Teach nested pattern matching in a new exercise

Open
#2,150 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
rust
Domain
content

Research direction

Read CONTRIBUTING.md and review the proposed Rust Playground exercise, focusing on how nested pattern matching is introduced for newcomers. Done means determining whether this exercise fits Rustlings, adapting it with the required explanatory comments, and preparing the contribution described in the guidelines.

Written by the indexing model from the issue text.

Description

A-exercises C-new-exercise

From the discussion in #2127, @mo8it suggested to open a issue for tracking the inclusion of an exercise for teaching nested pattern matching.

It's been a while since that discussion, but no issue was opened to track this; hence, I opened an issue to track it.

I myself recently finished the Rustlings exercises and I came up with the below execrise which I think will help teach the concept of nested pattern matching to newcomers.

I'm sharing the source code, below:

// Author: Rishit Chaudhary
// Date: 9th November 2024

enum Color {
    Rgb(u8, u8, u8),
    Hsl(u8, u8, u8),
}

struct Position {
    x: i32,
    y: i32,
}

enum Command {
    Quit,
    Move(Position),
    Write(String, Position),
    ChangeColor(Color, Position),
}

fn main() {
    let cmds = &[
        Command::ChangeColor(Color::Hsl(0, 160, 255), Position { x: 1, y: 10 }),
        Command::ChangeColor(Color::Rgb(0, 160, 255), Position { x: 1, y: 10 }),
        Command::Move(Position { x: 1, y: 10 }),
        Command::Write("Hello".into(), Position { x: 1, y: 10 }),
        Command::Quit,
    ];

    for cmd in cmds {
        match cmd {
            Command::ChangeColor(Color::Rgb(r, g, b), Position { x, y }) => {
                println!("Change color of position ({x}, {y}) to red {r}, green {g}, and blue {b}");
            }
            Command::ChangeColor(Color::Hsl(h, s, l), Position { x, y }) => {
                println!(
                    "Change color of position ({x}, {y}) to hue {h}, saturation {s}, lightness {l}"
                )
            }
            Command::Write(text, Position { x, y }) => {
                println!("Write text \"{text}\" to position ({x}, {y}).")
            }
            Command::Move(Position { x, y }) => {
                println!("Move to position ({x}, {y}).")
            }
            Command::Quit => {
                println!("Goodbye!")
            }
        }
    }
}

Here's the Rust Playground link to the above code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=651e559640d308b05967d2aef512bbbe

If you think this code can be added as a new exercise; I'd be happy to add the required comments explaining the exercise, and create a PR to contribute it as per the guidelines mentioned in Contributing to Rustlings.

Let me know if you have any questions or suggestions, I'd be happy to help

Thanks

Dominant language
Rust
Stars
64.6k
Forks
11.3k
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rust-lang/rustlings

All issues in rust-lang/rustlings

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.