Skip to content

Commit 5547101

Browse files
authored
Rollup merge of #108094 - kornelski:fsdocs, r=cuviper
Demonstrate I/O in File examples I've noticed that some Rust novices unnecessarily reinvent `std::fs::{read,write}`, presumably because they search for equivalents of `fopen` + `fwrite`. I've added links to `std::fs::{read,write}` in the docs. The `File` examples were only showing how to open a file, but not how to use the opened handle, unnecessarily leaving out the next step. I've added a variety of different uses of file handles to their examples in docs.
2 parents 863cd15 + 4c2d48e commit 5547101

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

library/std/src/fs.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,10 @@ impl File {
334334
///
335335
/// See the [`OpenOptions::open`] method for more details.
336336
///
337+
/// If you only need to read the entire file contents,
338+
/// consider [`std::fs::read()`][self::read] or
339+
/// [`std::fs::read_to_string()`][self::read_to_string] instead.
340+
///
337341
/// # Errors
338342
///
339343
/// This function will return an error if `path` does not already exist.
@@ -343,9 +347,12 @@ impl File {
343347
///
344348
/// ```no_run
345349
/// use std::fs::File;
350+
/// use std::io::Read;
346351
///
347352
/// fn main() -> std::io::Result<()> {
348353
/// let mut f = File::open("foo.txt")?;
354+
/// let mut data = vec![];
355+
/// f.read_to_end(&mut data)?;
349356
/// Ok(())
350357
/// }
351358
/// ```
@@ -361,16 +368,20 @@ impl File {
361368
///
362369
/// Depending on the platform, this function may fail if the
363370
/// full directory path does not exist.
364-
///
365371
/// See the [`OpenOptions::open`] function for more details.
366372
///
373+
/// See also [`std::fs::write()`][self::write] for a simple function to
374+
/// create a file with a given data.
375+
///
367376
/// # Examples
368377
///
369378
/// ```no_run
370379
/// use std::fs::File;
380+
/// use std::io::Write;
371381
///
372382
/// fn main() -> std::io::Result<()> {
373383
/// let mut f = File::create("foo.txt")?;
384+
/// f.write_all(&1234_u32.to_be_bytes())?;
374385
/// Ok(())
375386
/// }
376387
/// ```
@@ -397,9 +408,11 @@ impl File {
397408
/// #![feature(file_create_new)]
398409
///
399410
/// use std::fs::File;
411+
/// use std::io::Write;
400412
///
401413
/// fn main() -> std::io::Result<()> {
402414
/// let mut f = File::create_new("foo.txt")?;
415+
/// f.write_all("Hello, world!".as_bytes())?;
403416
/// Ok(())
404417
/// }
405418
/// ```
@@ -426,9 +439,11 @@ impl File {
426439
///
427440
/// ```no_run
428441
/// use std::fs::File;
442+
/// use std::io::Write;
429443
///
430444
/// fn main() -> std::io::Result<()> {
431445
/// let mut f = File::options().append(true).open("example.log")?;
446+
/// writeln!(&mut f, "new line")?;
432447
/// Ok(())
433448
/// }
434449
/// ```
@@ -966,6 +981,9 @@ impl OpenOptions {
966981
/// In order for the file to be created, [`OpenOptions::write`] or
967982
/// [`OpenOptions::append`] access must be used.
968983
///
984+
/// See also [`std::fs::write()`][self::write] for a simple function to
985+
/// create a file with a given data.
986+
///
969987
/// # Examples
970988
///
971989
/// ```no_run

0 commit comments

Comments
 (0)