Skip to content

Vector: Creating empty vectors returns an error #35

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

Open
palash25 opened this issue May 8, 2019 · 1 comment
Open

Vector: Creating empty vectors returns an error #35

palash25 opened this issue May 8, 2019 · 1 comment

Comments

@palash25
Copy link

palash25 commented May 8, 2019

In the very first example in the Vectors chapter

let mut a = Vec::new(); //1.With new() keyword
let mut b = vec![]; //2.Using the vec! macro

when I compile just these statements I get an error saying

error[E0282]: type annotations needed
  --> a.rs:19:15
   |
19 |     let mut bv = vec![]; //2.Using the vec! macro
   |         ------   ^^^^^^ cannot infer type for `T`
   |         |
   |         consider giving `bv` a type
   |
   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

Shouldn't this be documented in the guide?

@vks
Copy link
Contributor

vks commented Apr 9, 2021

I agree it would be nice to add a comment. However, the compiler advice is very clear in this case?

If you later push an element into the vector, the type annotation is usually not necessary, and you don't get this error.

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

No branches or pull requests

2 participants