@@ -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 [`std::fs::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 [`std::fs::read`]) :
46
46
///
47
47
/// ```no_run
48
48
/// use std::fs::File;
@@ -397,6 +397,37 @@ impl File {
397
397
OpenOptions :: new ( ) . write ( true ) . create ( true ) . truncate ( true ) . open ( path. as_ref ( ) )
398
398
}
399
399
400
+ /// Returns a new OpenOptions object.
401
+ ///
402
+ /// This function returns a new OpenOptions object that you can use to
403
+ /// open or create a file with specific options if `open()` or `create()`
404
+ /// are not appropriate.
405
+ ///
406
+ /// It is equivalent to `OpenOptions::new()` but allows you to write more
407
+ /// readable code. Instead of `OpenOptions::new().read(true).open("foo.txt")`
408
+ /// you can write `File::with_options().read(true).open("foo.txt"). This
409
+ /// also avoids the need to import `OpenOptions`.
410
+ ///
411
+ /// See the [`OpenOptions::new`] function for more details.
412
+ ///
413
+ /// [`OpenOptions::new`]: struct.OpenOptions.html#method.new
414
+ ///
415
+ /// # Examples
416
+ ///
417
+ /// ```no_run
418
+ /// #![feature(with_options)]
419
+ /// use std::fs::File;
420
+ ///
421
+ /// fn main() -> std::io::Result<()> {
422
+ /// let mut f = File::with_options().read(true).open("foo.txt")?;
423
+ /// Ok(())
424
+ /// }
425
+ /// ```
426
+ #[ unstable( feature = "with_options" , issue = "65439" ) ]
427
+ pub fn with_options ( ) -> OpenOptions {
428
+ OpenOptions :: new ( )
429
+ }
430
+
400
431
/// Attempts to sync all OS-internal metadata to disk.
401
432
///
402
433
/// This function will attempt to ensure that all in-memory data reaches the
0 commit comments