-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add fs::read_to_string #34857
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
Comments
What if we had a Oh also, there is already a |
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 { /* ... */ }
}
|
An BTW is trying to read the whole file into memory an anti-pattern? Apparently |
@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. |
Libs team discussed and is amenable to a convenience, though the design may need some consideration still. Some concrete points brought up:
|
Can we just |
@kvark That would work for files, but would conflict with instances of Into for other implementations of Read, notably the implementation for String. |
There's a crate for that: https://github.com/pornel/rust-file |
For naming, how about |
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. |
Uh oh!
There was an error while loading. Please reload this page.
Every time I want to load the contents of a file to a
String
I 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 theRead
trait 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.
The text was updated successfully, but these errors were encountered: