`match for` syntax to simplify matching across a for loop
Nobody has claimed this yet.
- Dominant language
- Markdown
- Stars
- 6.6k
- Forks
- 1.7k
- Avg merge
- 16h 14m
- Merged PRs (30d)
- 1
Description
I'm offering a simple suggestion for improving the readability of Rust code. If you think it's a bad idea, feel free to close the issue. I've noticed a pattern throughout my experience of writing code in Rust that could be better written with a simpler syntax, match for. The current syntax for writing a match for loop is as follows:
for token in pattern {
match token {
// matching code
}
}
However, I am suggesting that it might be a good idea to also implement the following which will serve as syntactic sugar for the above and will eliminate the redundancy:
match for token in pattern {
// matching code
}
As an example in some real world code, I currently have the following:
for pattern in self.template.clone() {
match pattern {
TemplateToken::Character(value) => filename.push(value),
TemplateToken::Series => if !self.no_name { filename.push_str(self.series_name.clone().as_str()); },
TemplateToken::Season => filename.push_str(self.season_number.to_string().as_str()),
TemplateToken::Episode => filename.push_str(episode.to_padded_string('0', self.pad_length).as_str()),
TemplateToken::Title => if self.tvdb { filename.push_str(title); }
}
}
However, it could be better written as the following simplified form:
match for pattern in self.template.clone() {
TemplateToken::Character(value) => filename.push(value),
TemplateToken::Series => if !self.no_name { filename.push_str(self.series_name.clone().as_str()); },
TemplateToken::Season => filename.push_str(self.season_number.to_string().as_str()),
TemplateToken::Episode => filename.push_str(episode.to_padded_string('0', self.pad_length).as_str()),
TemplateToken::Title => if self.tvdb { filename.push_str(title); }
}
Contributor guide
No contributing guide indexed for this repository
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.
Research direction
Start by reviewing the proposed match for examples and compare them with the existing for-then-match form. Determine whether the suggested syntax fits Rust's language design and RFC process; done means reaching a documented decision on the proposal.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 25/100