Teach nested pattern matching in a new exercise
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 45/100
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
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
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from rust-lang/rustlings
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
-
A-exercises C-enhancement P-low
Difficulty 1/5 Under an hour Newbie friendliness 76/100
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
-
Proposal: German (Deutsch) translation for Community ExercisesMay be free again @mo8it claimed this 62 days ago, and no pull request is open. Open
rust-lang/rustlings#2445 · 4 comments · 2 reactions · 1 assignee ·
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
All issues in rust-lang/rustlings
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
area:frontend kind:bug priority:p3 ready
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 1 day
-
bug priority: medium ui
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
storytold/vectorcraft#6333 · 1 comment ·
Maintainers usually reply within 1 day
-
bug lexer
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
yara-x fails to build with rsa 0.10.0: unresolved import rsa::Pkcs1v15SignPossibly taken @CfirTsabari claimed this today. Open
Difficulty 1/5 Under an hour Newbie friendliness 87/100
VirusTotal/yara-x#865 ·
Maintainers usually reply within 1 day