Skip to content

Concisely creating a vector of a known capacity #11330

Closed
@ktnlvr

Description

@ktnlvr

What it does

Informs the user about a more concise way to create a vector with a known capacity.

I often wrote code like the following:

{ 
    let mut v = vec![];
    v.reserve(space_hint);
    v
}

since I didn't know about the existence of Vec::with_capacity, maybe clippy can warn about things like that? It also seems like this can be generalized to cases when we are pushing into an empty vector for a known amount of times.

Advantage

No response

Drawbacks

No response

Example

{ 
    let mut v = vec![];
    v.reserve(space_hint);
    v
}

Could be written as:

Vec::with_capacity(space_hint)

Metadata

Metadata

Assignees

Labels

A-lintArea: New lintsgood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions