Closed
Description
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
- Implementation: Add a
std::io::read_to_string
function #80217FCPStabilization PR (Stabilizestd::io::read_to_string
#100337)
Unresolved Questions
- Function signature (Remove
&mut
fromio::read_to_string
signature #92863)
Metadata
Metadata
Assignees
Labels
Area: `std::io`, `std::fs`, `std::net` and `std::path`Category: An issue tracking the progress of sth. like the implementation of an RFCLibs issues that are tracked on the team's project board.Relevant to the library API team, which will review and decide on the PR/issue.This issue / PR is in PFCP or FCP with a disposition to merge it.The final comment period is finished for this PR / Issue.
Activity
[-]Tracking Issue for `io::read_to_string`[/-][+]Tracking Issue for `std::io::read_to_string`[/+]kartva commentedon Jun 4, 2021
What is the status on stabilizing this addition to the library?
fee1-dead commentedon Jun 23, 2021
@m-ou-se / @rust-lang/libs
Can we start an FCP?
m-ou-se commentedon Jul 5, 2021
Thinking a bit about the signature: Considering
Read
is already implemented for&mut impl Read
, shouldn't this function just take anR
instead of&mut R
?camelid commentedon Jul 9, 2021
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 forstd
here, so if that works, it seems okay to me.jkugelman commentedon Oct 6, 2021
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 makeio::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?
camelid commentedon Oct 6, 2021
That's great; thanks for working on this!
Perhaps we could add another paragraph at the end like this:
jkugelman commentedon Oct 6, 2021
Done.
jkugelman commentedon Oct 6, 2021
It seems like
std::io::read_to_string
should be paired with astd::io::read_to_end
method to match the other pairings in the standard library:Read::read_to_end
+Read::read_to_string
fs::read
+fs::read_to_string
Would it be
read
orread_to_end
? I'd sayread_to_end
to match theRead
trait since the reader might not be at the beginning.32 remaining items