Skip to content

Tracking Issue for std::io::read_to_string #80218

Closed
@camelid

Description

@camelid
Member

Feature gate: #![feature(io_read_to_string)]

This is a tracking issue for the std::io::read_to_string function.

The equivalent of std::fs::read_to_string, but generalized to all
Read impls.

As the documentation on std::io::read_to_string says, the advantage of
this function is that it means you don't have to create a variable first
and it provides more type safety since you can only get the buffer out
if there were no errors. If you use Read::read_to_string, you have to
remember to check whether the read succeeded because otherwise your
buffer will be empty.

It's friendlier to newcomers and better in most cases to use an explicit
return value instead of an out parameter.

Public API

pub fn read_to_string<R: Read>(reader: R) -> Result<String>;

Steps / History

Unresolved Questions

Activity

changed the title [-]Tracking Issue for `io::read_to_string`[/-] [+]Tracking Issue for `std::io::read_to_string`[/+] on Dec 20, 2020
added
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
A-ioArea: `std::io`, `std::fs`, `std::net` and `std::path`
on Dec 20, 2020
added
Libs-TrackedLibs issues that are tracked on the team's project board.
on Jan 6, 2021
kartva

kartva commented on Jun 4, 2021

@kartva
Contributor

What is the status on stabilizing this addition to the library?

fee1-dead

fee1-dead commented on Jun 23, 2021

@fee1-dead
Member

@m-ou-se / @rust-lang/libs

Can we start an FCP?

m-ou-se

m-ou-se commented on Jul 5, 2021

@m-ou-se
Member

Thinking a bit about the signature: Considering Read is already implemented for &mut impl Read, shouldn't this function just take an R instead of &mut R?

camelid

camelid commented on Jul 9, 2021

@camelid
MemberAuthor

Thinking a bit about the signature: Considering Read is already implemented for &mut impl Read, shouldn't this function just take an R instead of &mut R?

I modeled it after Read::read_to_string, which takes an &mut self, but that has to use &mut self because it's defined as part of the trait. I don't know enough about the idiom for std here, so if that works, it seems okay to me.

jkugelman

jkugelman commented on Oct 6, 2021

@jkugelman
Contributor

I'm working on a related PR #89582 to have File::read_to_string enlarge the string buffer before reading to accommodate the full file size. A nice side effect is that it will make io::read_to_string more performant when the reader is a file.

If it lands do you think there's anything in this comment that should be updated?

Performance

The downside of this function's increased ease of use and type safety is that it gives you less control over performance. For example, you can't pre-allocate memory like you can using String::with_capacity and Read::read_to_string. Also, you can't re-use the buffer if an error occurs while reading.

In many cases, this function's performance will be adequate and the ease of use and type safety tradeoffs will be worth it. However, there are cases where you need more control over performance, and in those cases you should definitely use Read::read_to_string directly.

camelid

camelid commented on Oct 6, 2021

@camelid
MemberAuthor

I'm working on a related PR #89582 to have File::read_to_string enlarge the string buffer before reading to accommodate the full file size. A nice side effect is that it will make io::read_to_string more performant when the reader is a file.

That's great; thanks for working on this!

If it lands do you think there's anything in this comment that should be updated?

Perhaps we could add another paragraph at the end like this:

Note that in some special cases, such as when reading files, this function will pre-allocate memory based on the size of the input is reading. In those cases, the performance should be as good as if you had used Read::read_to_string() with a manually pre-allocated buffer.

jkugelman

jkugelman commented on Oct 6, 2021

@jkugelman
Contributor

Done.

jkugelman

jkugelman commented on Oct 6, 2021

@jkugelman
Contributor

It seems like std::io::read_to_string should be paired with a std::io::read_to_end method to match the other pairings in the standard library:

Would it be read or read_to_end? I'd say read_to_end to match the Read trait since the reader might not be at the beginning.

32 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-ioArea: `std::io`, `std::fs`, `std::net` and `std::path`C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCLibs-TrackedLibs issues that are tracked on the team's project board.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @joshtriplett@m-ou-se@jkugelman@apiraino@rfcbot

    Issue actions

      Tracking Issue for `std::io::read_to_string` · Issue #80218 · rust-lang/rust