Skip to content

Bad help message when calling collect on a Cow<str> iterator #144100

@ChrisDenton

Description

@ChrisDenton

Code

use std::borrow::Cow;

fn main() {
    let a: Cow<'_, str> = Cow::from("hello
how
are
you?
");
    let lines: Vec<String> = a.lines().collect();
}

Current output

error[E0277]: a value of type `Vec<String>` cannot be built from an iterator over elements of type `&str`
    --> src/main.rs:9:40
     |
9    |     let lines: Vec<String> = a.lines().collect();
     |                                        ^^^^^^^ value of type `Vec<String>` cannot be built from `std::iter::Iterator<Item=&str>`
     |
     = help: the trait `FromIterator<&str>` is not implemented for `Vec<String>`
             but trait `FromIterator<String>` is implemented for it
     = help: for that trait implementation, expected `String`, found `&str`
note: the method call chain might not have had the expected associated types
    --> src/main.rs:9:32
     |
4    |       let a: Cow<'_, str> = Cow::from("hello
     |  ___________________________-
5    | | how
6    | | are
7    | | you?
8    | | ");
     | |__- this expression has type `Cow<'_, str>`
9    |       let lines: Vec<String> = a.lines().collect();
     |                                  ^^^^^^^ `Iterator::Item` is `&str` here
note: required by a bound in `collect`
    --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:2014:19
     |
2014 |     fn collect<B: FromIterator<Self::Item>>(self) -> B
     |                   ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Iterator::collect`
help: consider removing this method call, as the receiver has type `Cow<'_, str>` and `Cow<'_, str>: FromIterator<&str>` trivially holds
     |
9    -     let lines: Vec<String> = a.lines().collect();
9    +     let lines: Vec<String> = a.collect();
     |

Desired output

error[E0277]: a value of type `Vec<String>` cannot be built from an iterator over elements of type `&str`
    --> src/main.rs:9:40
     |
9    |     let lines: Vec<String> = a.lines().collect();
     |                                        ^^^^^^^ value of type `Vec<String>` cannot be built from `std::iter::Iterator<Item=&str>`
     |
     = help: the trait `FromIterator<&str>` is not implemented for `Vec<String>`
             but trait `FromIterator<String>` is implemented for it
     = help: for that trait implementation, expected `String`, found `&str`
note: the method call chain might not have had the expected associated types
    --> src/main.rs:9:32
     |
4    |       let a: Cow<'_, str> = Cow::from("hello
     |  ___________________________-
5    | | how
6    | | are
7    | | you?
8    | | ");
     | |__- this expression has type `Cow<'_, str>`
9    |       let lines: Vec<String> = a.lines().collect();
     |                                  ^^^^^^^ `Iterator::Item` is `&str` here
note: required by a bound in `collect`
    --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:2014:19
     |
2014 |     fn collect<B: FromIterator<Self::Item>>(self) -> B
     |                   ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Iterator::collect`

Rationale and extra context

Suggesting that the user remove .lines() from the code changes the intended meaning and is not a correct fix.

We should either drop the suggestion or be smarter about suggesting adding map or something,

Other cases

Rust Version

rustc 1.90.0-nightly (5795086bd 2025-07-16)
binary: rustc
commit-hash: 5795086bdfe7ed988aa53a110bd0692c33d8755b
commit-date: 2025-07-16
host: x86_64-pc-windows-msvc
release: 1.90.0-nightly
LLVM version: 20.1.8

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

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