@@ -29,7 +29,7 @@ use crate::time::SystemTime;
2929///
3030/// # Examples
3131///
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`]) :
3333///
3434/// ```no_run
3535/// use std::fs::File;
@@ -42,7 +42,7 @@ use crate::time::SystemTime;
4242/// }
4343/// ```
4444///
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`]) :
4646///
4747/// ```no_run
4848/// use std::fs::File;
@@ -397,6 +397,37 @@ impl File {
397397 OpenOptions :: new ( ) . write ( true ) . create ( true ) . truncate ( true ) . open ( path. as_ref ( ) )
398398 }
399399
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+
400431 /// Attempts to sync all OS-internal metadata to disk.
401432 ///
402433 /// This function will attempt to ensure that all in-memory data reaches the
0 commit comments