rust-lang / rust-lang/rust-clippy
Recommend <collection type>::from
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
I discovered HashMap::from a bit late, and wish clippy had encouraged me to use this helpful constructor function rather than the collect-based boilerplate I have been using so far. I think I was cribbing some of my Rust knowledge from older tutorials.
Advantage
Discourages unnecessary boilerplate
Reduces mistakes
Encourages higher quality code
Drawbacks
No response
Example
let m = HashMap<String, String> = vec![
("bsdmakefile", "bmake"),
("gnumakefile", "gmake"),
("makefile", "make"),
]
.into_iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect::<HashMap<String, String>>();
Could be written as:
let m = HashMap<String, String>from([
("bsdmakefile", "bmake"),
("gnumakefile", "gmake"),
("makefile", "make"),
];
Contributor 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.
Research direction
No implementation files, tests, or entry points are named. Start by reviewing the proposed collect-based example and the requested HashMap::from recommendation; done means Clippy identifies the applicable boilerplate and suggests the collection constructor without changing unrelated code.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100