Skip to content

Commit a34e87f

Browse files
committed
Add Examples for File
This is pretty basic, but it's nice to have something.
1 parent b0aad7d commit a34e87f

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

src/libstd/fs/mod.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ impl File {
128128
///
129129
/// This function will return an error if `path` does not already exist.
130130
/// Other errors may also be returned according to `OpenOptions::open`.
131+
///
132+
/// # Examples
133+
///
134+
/// ```no_run
135+
/// use std::fs::File;
136+
///
137+
/// # fn foo() -> std::io::Result<()> {
138+
/// let mut f = try!(File::open("foo.txt"));
139+
/// # Ok(())
140+
/// # }
141+
/// ```
131142
#[stable(feature = "rust1", since = "1.0.0")]
132143
pub fn open<P: AsPath>(path: P) -> io::Result<File> {
133144
OpenOptions::new().read(true).open(path)
@@ -139,6 +150,17 @@ impl File {
139150
/// and will truncate it if it does.
140151
///
141152
/// See the `OpenOptions::open` function for more details.
153+
///
154+
/// # Examples
155+
///
156+
/// ```no_run
157+
/// use std::fs::File;
158+
///
159+
/// # fn foo() -> std::io::Result<()> {
160+
/// let mut f = try!(File::create("foo.txt"));
161+
/// # Ok(())
162+
/// # }
163+
/// ```
142164
#[stable(feature = "rust1", since = "1.0.0")]
143165
pub fn create<P: AsPath>(path: P) -> io::Result<File> {
144166
OpenOptions::new().write(true).create(true).truncate(true).open(path)
@@ -156,6 +178,20 @@ impl File {
156178
///
157179
/// This function will attempt to ensure that all in-core data reaches the
158180
/// filesystem before returning.
181+
///
182+
/// # Examples
183+
///
184+
/// ```no_run
185+
/// use std::fs::File;
186+
///
187+
/// # fn foo() -> std::io::Result<()> {
188+
/// let mut f = try!(File::create("foo.txt"));
189+
/// try!(f.write_all(b"Hello, world!"));
190+
///
191+
/// try!(f.sync_all());
192+
/// # Ok(())
193+
/// # }
194+
/// ```
159195
#[stable(feature = "rust1", since = "1.0.0")]
160196
pub fn sync_all(&self) -> io::Result<()> {
161197
self.inner.fsync()
@@ -170,6 +206,20 @@ impl File {
170206
///
171207
/// Note that some platforms may simply implement this in terms of
172208
/// `sync_all`.
209+
///
210+
/// # Examples
211+
///
212+
/// ```no_run
213+
/// use std::fs::File;
214+
///
215+
/// # fn foo() -> std::io::Result<()> {
216+
/// let mut f = try!(File::create("foo.txt"));
217+
/// try!(f.write_all(b"Hello, world!"));
218+
///
219+
/// try!(f.sync_data());
220+
/// # Ok(())
221+
/// # }
222+
/// ```
173223
#[stable(feature = "rust1", since = "1.0.0")]
174224
pub fn sync_data(&self) -> io::Result<()> {
175225
self.inner.datasync()
@@ -182,12 +232,36 @@ impl File {
182232
/// be shrunk. If it is greater than the current file's size, then the file
183233
/// will be extended to `size` and have all of the intermediate data filled
184234
/// in with 0s.
235+
///
236+
/// # Examples
237+
///
238+
/// ```no_run
239+
/// use std::fs::File;
240+
///
241+
/// # fn foo() -> std::io::Result<()> {
242+
/// let mut f = try!(File::open("foo.txt"));
243+
/// try!(f.set_len(0));
244+
/// # Ok(())
245+
/// # }
246+
/// ```
185247
#[stable(feature = "rust1", since = "1.0.0")]
186248
pub fn set_len(&self, size: u64) -> io::Result<()> {
187249
self.inner.truncate(size)
188250
}
189251

190252
/// Queries metadata about the underlying file.
253+
///
254+
/// # Examples
255+
///
256+
/// ```no_run
257+
/// use std::fs::File;
258+
///
259+
/// # fn foo() -> std::io::Result<()> {
260+
/// let mut f = try!(File::open("foo.txt"));
261+
/// let metadata = try!(f.metadata());
262+
/// # Ok(())
263+
/// # }
264+
/// ```
191265
#[stable(feature = "rust1", since = "1.0.0")]
192266
pub fn metadata(&self) -> io::Result<Metadata> {
193267
self.inner.file_attr().map(Metadata)

0 commit comments

Comments
 (0)