Skip to content

Join does not work on borrowed Strings #82910

@fugu13

Description

@fugu13

I tried this code:

let errors = vec![&String::from("hi"), &String::from("world")];
let combined = errors.join(" ");

(This is a contrived example, of course, what I was really doing was borrowing from inside a parsed data structure, which led to the &Strings).

I expected to see this happen: the strings to join together, just as if they were &str or String

Instead, this happened: I received the following error

error[E0599]: the method `join` exists for struct `Vec<&std::string::String>`, but its trait bounds were not satisfied
   --> src/main.rs:103:35
    |
103 |             let combined = errors.join(" ; ");
    |                                   ^^^^ method cannot be called on `Vec<&std::string::String>` due to unsatisfied trait bounds
    |
    = note: the following trait bounds were not satisfied:
            `<[&std::string::String] as Join<_>>::Output = _`

(Which took me a good while to work out what I needed to do even looking at the source code for Join and such, as this was extremely opaque to someone fairly new to Rust).

Meta

rustc --version --verbose:

rustc 1.52.0-nightly (35dbef235 2021-03-02)
binary: rustc
commit-hash: 35dbef235048f9a2939dc20effe083ca483c37ff
commit-date: 2021-03-02
host: x86_64-apple-darwin
release: 1.52.0-nightly
LLVM version: 11.0.1
Backtrace

error[E0599]: the method `join` exists for struct `Vec<&std::string::String>`, but its trait bounds were not satisfied
   --> src/main.rs:103:35
    |
103 |             let combined = errors.join(" ; ");
    |                                   ^^^^ method cannot be called on `Vec<&std::string::String>` due to unsatisfied trait bounds
    |
    = note: the following trait bounds were not satisfied:
            `<[&std::string::String] as Join<_>>::Output = _`

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-libs-apiRelevant to the library API 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