Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterator::intersperse doesn't seem to work correctly when collecting into a String #81145

Closed
PatchMixolydic opened this issue Jan 18, 2021 · 0 comments · Fixed by #81152
Closed
Labels
A-iterators Area: Iterators C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@PatchMixolydic
Copy link
Contributor

PatchMixolydic commented Jan 18, 2021

I tried this code (playground):

#![feature(iter_intersperse)]

fn main() {
    let contents = vec![1, 2, 3];

    let contents_string = contents
        .into_iter()
        .map(|id| id.to_string())
        .intersperse(", ".to_owned())
        .collect::<String>();

    println!("{}", contents_string);
}

I expected to see this happen: ", " is placed between each digit:

1, 2, 3

Instead, this happened: intersperse does not insert ", " between "1" and "2":

12, 3

Note that this works as expected when collecting into a Vec (playground):

#![feature(iter_intersperse)]

fn main() {
    let contents = vec![1, 2, 3];

    let contents_strings = contents
        .into_iter()
        .map(|id| id.to_string())
        .intersperse(", ".to_owned())
        .collect::<Vec<_>>();

    println!("{:?}", contents_strings);
}
["1", ", ", "2", ", ", "3"]

Meta

rustc --version --verbose:

rustc 1.51.0-nightly (8a6518427 2021-01-16)
binary: rustc
commit-hash: 8a6518427e11e6dd13d6f39663b82eb4f810ca05
commit-date: 2021-01-16
host: x86_64-unknown-linux-gnu
release: 1.51.0-nightly
LLVM version: 11.0.1

@rustbot modify labels: +A-iterators +T-libs

@PatchMixolydic PatchMixolydic added the C-bug Category: This is a bug. label Jan 18, 2021
@rustbot rustbot added A-iterators Area: Iterators T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Jan 18, 2021
@PatchMixolydic PatchMixolydic changed the title Iterator::intersperse doesn't seem to work correctly when collecting into a string Iterator::intersperse doesn't seem to work correctly when collecting into a String Jan 18, 2021
m-ou-se added a commit to m-ou-se/rust that referenced this issue Jan 20, 2021
m-ou-se added a commit to m-ou-se/rust that referenced this issue Jan 20, 2021
@bors bors closed this as completed in 202720b Jan 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-iterators Area: Iterators C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants