@@ -29,7 +29,7 @@ use crate::time::SystemTime;
29
29
///
30
30
/// # Examples
31
31
///
32
- /// Creates a new file and write bytes to it:
32
+ /// Creates a new file and write bytes to it (you can also use [`write`]) :
33
33
///
34
34
/// ```no_run
35
35
/// use std::fs::File;
@@ -42,7 +42,7 @@ use crate::time::SystemTime;
42
42
/// }
43
43
/// ```
44
44
///
45
- /// Read the contents of a file into a [`String`]:
45
+ /// Read the contents of a file into a [`String`] (you can also use [`read`]) :
46
46
///
47
47
/// ```no_run
48
48
/// use std::fs::File;
@@ -89,6 +89,8 @@ use crate::time::SystemTime;
89
89
/// [`Write`]: ../io/trait.Write.html
90
90
/// [`BufReader<R>`]: ../io/struct.BufReader.html
91
91
/// [`sync_all`]: struct.File.html#method.sync_all
92
+ /// [`read`]: fn.read.html
93
+ /// [`write`]: fn.write.html
92
94
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
93
95
pub struct File {
94
96
inner : fs_imp:: File ,
@@ -397,6 +399,37 @@ impl File {
397
399
OpenOptions :: new ( ) . write ( true ) . create ( true ) . truncate ( true ) . open ( path. as_ref ( ) )
398
400
}
399
401
402
+ /// Returns a new OpenOptions object.
403
+ ///
404
+ /// This function returns a new OpenOptions object that you can use to
405
+ /// open or create a file with specific options if `open()` or `create()`
406
+ /// are not appropriate.
407
+ ///
408
+ /// It is equivalent to `OpenOptions::new()` but allows you to write more
409
+ /// readable code. Instead of `OpenOptions::new().read(true).open("foo.txt")`
410
+ /// you can write `File::with_options().read(true).open("foo.txt"). This
411
+ /// also avoids the need to import `OpenOptions`.
412
+ ///
413
+ /// See the [`OpenOptions::new`] function for more details.
414
+ ///
415
+ /// [`OpenOptions::new`]: struct.OpenOptions.html#method.new
416
+ ///
417
+ /// # Examples
418
+ ///
419
+ /// ```no_run
420
+ /// #![feature(with_options)]
421
+ /// use std::fs::File;
422
+ ///
423
+ /// fn main() -> std::io::Result<()> {
424
+ /// let mut f = File::with_options().read(true).open("foo.txt")?;
425
+ /// Ok(())
426
+ /// }
427
+ /// ```
428
+ #[ unstable( feature = "with_options" , issue = "65439" ) ]
429
+ pub fn with_options ( ) -> OpenOptions {
430
+ OpenOptions :: new ( )
431
+ }
432
+
400
433
/// Attempts to sync all OS-internal metadata to disk.
401
434
///
402
435
/// This function will attempt to ensure that all in-memory data reaches the
0 commit comments