Skip to content

Hint for Vec<String, String> #83250

Closed
@jsha

Description

@jsha

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=5f472854b25b6ccce15820ca0dd45c19

fn foo() {
    let _z: Vec<String, String> = vec![]; // A vector of string pairs, right?
}

The current output is:

error[E0658]: use of unstable library feature 'allocator_api'
 --> src/lib.rs:2:25
  |
2 |     let _z: Vec<String, String> = vec![]; // A vector of string pairs, right?
  |                         ^^^^^^
  |
  = note: see issue #32838 <https://github.com/rust-lang/rust/issues/32838> for more information

error[E0277]: the trait bound `String: Allocator` is not satisfied
   --> src/lib.rs:2:13
    |
2   |     let _z: Vec<String, String> = vec![]; // A vector of string pairs, right?
    |             ^^^^^^^^^^^^^^^^^^^ the trait `Allocator` is not implemented for `String`

error[E0308]: mismatched types
 --> src/lib.rs:2:35
  |
2 |     let _z: Vec<String, String> = vec![]; // A vector of string pairs, right?
  |             -------------------   ^^^^^^ expected struct `String`, found struct `std::alloc::Global`
  |             |
  |             expected due to this
  |
  = note: expected struct `Vec<String, String>`
             found struct `Vec<_, std::alloc::Global>`
  = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 3 previous errors

Ideally the output should look like:

The above, plus:

hint: for a Vec of String pairs, try Vec<(String, String)>

Rationale: The current error messages assume that users know Vec has a second type param, which is rarely used. A vector of pairs is a fairly common type, and I suspect a lot of people make the mistake of forgetting the parentheses.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions