Skip to content

str::lines consumes carriage returns at the end of a line which is undocumented #94435

Closed
@KamilaBorowska

Description

@KamilaBorowska

I tried this code:

fn main() {
    for line in "a\r".lines() {
        println!("{:?}", line);
    }
}

I expected to see this happen: "a\r" to be printed.

Instead, this happened: "a" is being printed.

At this point fixing this behavior may break actual code (and this would be a quiet change), however documenting this may make sense. This behavior was introduced in Rust 1.4.0 as a result of implementation of RFC 1212.

Curiously, BufRead::lines whose behavior was changed by the same RFC doesn't have this issue. It makes sure that \r is followed by \n before deleting the carriage return.

Meta

rustc --version --verbose:

rustc 1.59.0 (9d1b2106e 2022-02-23)
binary: rustc
commit-hash: 9d1b2106e23b1abd32fce1f17267604a5102f57a
commit-date: 2022-02-23
host: x86_64-unknown-linux-gnu
release: 1.59.0
LLVM version: 13.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions