Skip to content

Vector: Creating empty vectors returns an error #35

Open
@palash25

Description

@palash25

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?

Activity

vks

vks commented on Apr 9, 2021

@vks
Contributor

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @vks@palash25

        Issue actions

          Vector: Creating empty vectors returns an error · Issue #35 · learning-rust/learning-rust.github.io