Skip to content

Suggest adding & for "mismatched types: expected reference &&[&str], found reference &[&str]" #78613

Closed

Description

@camelid

This code can be easily fixed by adding an & in the call to contains.

fn main() {
    let haystack = [&["A1", "A2"][..], &["B1", "B2"], &["C1", "C2"]];
    let needle: &[&str] = &["D1", "D2"];
    println!("{}", haystack.contains(needle));
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/main.rs:4:38
  |
4 |     println!("{}", haystack.contains(needle));
  |                                      ^^^^^^ expected `&[&str]`, found slice `[&str]`
  |
  = note: expected reference `&&[&str]`
             found reference `&[&str]`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

Activity

  1. camelid commented on Oct 31, 2020

    @camelid
    MemberAuthor

    Also, I don't think autoref is documented anywhere.

  2. jonas-schievink commented on Oct 31, 2020

    @jonas-schievink
    Contributor

    Autoref doesn't happen for arguments, only for the receiver

  3. camelid commented on Oct 31, 2020

    @camelid
    MemberAuthor

    Maybe we could suggest adding an & then?

  4. changed the title [-]Mismatched types: expected reference `&&[&str]`, found reference `&[&str]`[/-] [+]Suggest adding `&` for "mismatched types: expected reference `&&[&str]`, found reference `&[&str]`"[/+] on Oct 31, 2020
  5. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    on Oct 31, 2020
  6. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Nov 3, 2020
  7. added
    D-papercutDiagnostics: An error or lint that needs small tweaks.
    D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.
    on Aug 3, 2023
  8. estebank commented on Aug 12, 2025

    @estebank
    Contributor

    Current output, little change, still no suggestion:

    error[E0308]: mismatched types
        --> src/main.rs:4:38
         |
       4 |     println!("{}", haystack.contains(needle));
         |                             -------- ^^^^^^ expected `&&[&str]`, found `&[&str]`
         |                             |
         |                             arguments to this method are incorrect
         |
         = note: expected reference `&&[&str]`
                    found reference `&[&str]`
    note: method defined here
        --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/slice/mod.rs:2583:12
         |
    2583 |     pub fn contains(&self, x: &T) -> bool
         |            ^^^^^^^^
    
  9. self-assigned this
    on Sep 5, 2026
  10. added 2 commits that reference this issue on Sep 6, 2026
    92f889e
    397c478
  11. added a commit that references this issue on Sep 6, 2026
    4037d19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceA-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.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.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