Skip to content

Failure to recognize &String as equivalent to &str #125450

Open
@amab8901

Description

Code

fn main() {
    let string_vec = vec!["1".to_string(), "2".to_string()];
    let string_iter = string_vec.iter().map(str_ref_to_string);
}

fn str_ref_to_string(str_arg: &str) -> String {
    str_arg.to_string()
}

Current output

type mismatch in function arguments
expected function signature `fn(&std::string::String) -> _`
   found function signature `fn(&str) -> _`rustc

Desired output

No error

Rationale and extra context

The compiler interprets &String as equivalent to &str in most cases, so I don't see why it can't do the same in this case.

Other cases

No response

Rust Version

rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: x86_64-unknown-linux-gnu
release: 1.78.0
LLVM version: 18.1.2

Anything else?

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-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