rust-lang / rust-lang/rust-clippy

Recommend <collection type>::from

Open
#10,856 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
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

Open the contributing guide

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.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.