Skip to content

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

New issue

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

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

Already on GitHub? # to your account

Closed
LunaBorowska opened this issue Feb 27, 2022 · 1 comment · Fixed by #100311
Closed
Labels
C-bug Category: This is a bug.

Comments

@LunaBorowska
Copy link
Contributor

LunaBorowska commented Feb 27, 2022

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
@LunaBorowska LunaBorowska added the C-bug Category: This is a bug. label Feb 27, 2022
@hkratz
Copy link
Contributor

hkratz commented Feb 28, 2022

There is a PR for it: #91191

LunaBorowska added a commit to LunaBorowska/rust that referenced this issue Aug 9, 2022
Previously "bare\r" was split into ["bare"] even though the
documentation said that only LF and CRLF count as newlines.

This fix is a behavioural change, even though it brings the behaviour
into line with the documentation, and into line with that of
`std::io::BufRead::lines()`.

This is an alternative to rust-lang#91051, which proposes to document rather
than fix the behaviour.

Fixes rust-lang#94435.

Co-authored-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 3, 2022
…, r=joshtriplett

Fix handling of trailing bare CR in str::lines

Continuing from rust-lang#91191.

Fixes rust-lang#94435.
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Mar 22, 2023
…, r=ChrisDenton

Fix handling of trailing bare CR in str::lines

Continuing from rust-lang#91191.

Fixes rust-lang#94435.
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Mar 22, 2023
…, r=ChrisDenton

Fix handling of trailing bare CR in str::lines

Continuing from rust-lang#91191.

Fixes rust-lang#94435.
@bors bors closed this as completed in cef81dc Mar 22, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants