Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd fs::read_to_string #34857
Comments
brson
added
A-libs
C-enhancement
I-nominated
T-libs
labels
Jul 16, 2016
This comment has been minimized.
This comment has been minimized.
|
What if we had a Oh also, there is already a |
This comment has been minimized.
This comment has been minimized.
|
Alternatively, we could make it generic: // Call as `fs::read::<String>("my_file")`. Alternatively, we could try landing default type arguments :)
mod fs {
fn read<T: SomeTrait, P: AsRef<Path>>(path: P) -> T { /* ... */ }
}
|
This comment has been minimized.
This comment has been minimized.
|
An BTW is trying to read the whole file into memory an anti-pattern? Apparently |
This comment has been minimized.
This comment has been minimized.
|
@gkoz I think this is one of those "pay for what you use" things. If you want to do things efficiently, manually read the file one chunk at a time; if you just need to read the file into memory and know it isn't malicious (e.g., a config file), you can use the helper function. However, your two caveats should definitely be mentioned in the documentation. |
brson
added
P-low
and removed
I-nominated
labels
Jul 18, 2016
This comment has been minimized.
This comment has been minimized.
|
Libs team discussed and is amenable to a convenience, though the design may need some consideration still. Some concrete points brought up:
|
This comment has been minimized.
This comment has been minimized.
|
Can we just |
This comment has been minimized.
This comment has been minimized.
|
@kvark That would work for files, but would conflict with instances of Into for other implementations of Read, notably the implementation for String. |
steveklabnik
removed
the
A-libs
label
Mar 24, 2017
This comment has been minimized.
This comment has been minimized.
|
There's a crate for that: https://github.com/pornel/rust-file |
This comment has been minimized.
This comment has been minimized.
binarybana
commented
May 6, 2017
|
For naming, how about |
This comment has been minimized.
This comment has been minimized.
|
The questions that were already brought up by the libs team and general uncertainty I can see about this lead me to believe it requires an RFC. As such, I'm closing -- if someone wants to pursue this, please follow the process outlined here: https://github.com/rust-lang/rfcs#before-creating-an-rfc. |
brson commentedJul 16, 2016
•
edited
Every time I want to load the contents of a file to a
StringI think this same thing: "there should be a fs::read_to_string".It looks like to me the most convenient way to do this once
?is stable (which doesn't seem to be near) might beFile::open(path)?.read_to_string()?, assuming you've got theReadtrait imported. It's just not as nice.We as @rust-lang/libs can be more aggressive about identifying and adding simple ergonomic improvements like this.