Skip to content

Use Vec::new instead of vec! #2712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2025
Merged

Use Vec::new instead of vec! #2712

merged 1 commit into from
Apr 23, 2025

Conversation

randomPoison
Copy link
Collaborator

Using the vec! macro to create an empty Vec is a bit weird imo, generally I only see the macro used when you actually want to initialize the Vec with some values (like is done in the examples for this exercise). Students are more likely to use Vec::new, and I think that's the more idiomatic approach, so I think using Vec::new here would be better.

@djmitche
Copy link
Collaborator

I tend to use vec![] just because it's fewer characters, but you're right that doing so is unusual.

Also, bummer, I appear to not be able to merge things anymore.

@michael-kerscher
Copy link
Collaborator

michael-kerscher commented Apr 23, 2025

There does not seem to be any official guideline for choosing between these two alternatives as far as I can find (rust-lang/style-team#70)

I also tend to use vec![] because this is shorter and it feels more similar to how I initialize a Vec with values.

let empty_function = Vec::new();
let empty_macro = vec![];
let values = vec![1,2,3];

But in the end I don't really mind how this is written and the code in the background is identical (if I disregard some fixed bugreports on different code being emitted for vec![] and Vec::new()...)

There is even a request for linting on vec![] usage but this was not done: rust-lang/rust-clippy#12774.

@michael-kerscher michael-kerscher merged commit 3726918 into main Apr 23, 2025
69 of 70 checks passed
@michael-kerscher michael-kerscher deleted the legare/death-to-vec branch April 23, 2025 09:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants