Skip to content

Supplying an &&str when an &str is expected missing a suggestion #66023

Open
@gilescope

Description

@gilescope
    let a = std::collections::HashMap::<String,String>::new();
    let s = "hello";
    let _b = a[&s];
               ^ & not needed, this is already a reference

playground

But the error we currently get is:

error[E0277]: the trait bound `std::string::String: std::borrow::Borrow<&str>` is not satisfied
 --> src/main.rs:4:14
  |
4 |     let _b = a[&s];
  |              ^^^^^ the trait `std::borrow::Borrow<&str>` is not implemented for `std::string::String`
  |
  = help: the following implementations were found:
            <std::string::String as std::borrow::Borrow<str>>
  = note: required because of the requirements on the impl of `std::ops::Index<&&str>` for `std::collections::HashMap<std::string::String, std::string::String>`

Adding one too many layers of indirection is fairly common in rust especially with String / &str so it would be excellent if Rust could hint to the user how to fix this.

Metadata

Metadata

Assignees

No one assigned

    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