Skip to content

Commit

Permalink
std: io: add some code examples
Browse files Browse the repository at this point in the history
Closes #11232.
  • Loading branch information
adrientetar committed Jan 4, 2014
1 parent ba80157 commit 95ace50
Showing 1 changed file with 45 additions and 21 deletions.
66 changes: 45 additions & 21 deletions src/libstd/io/mod.rs
Expand Up @@ -186,23 +186,29 @@ while still providing feedback about errors. The basic strategy:
so that nullable values do not have to be 'unwrapped' before use.
These features combine in the API to allow for expressions like
`File::new("diary.txt").write_line("met a girl")` without having to
worry about whether "diary.txt" exists or whether the write
succeeds. As written, if either `new` or `write_line` encounters
an error the task will fail.
If you wanted to handle the error though you might write
let mut error = None;
do io_error::cond(|e: IoError| {
error = Some(e);
}).in {
File::new("diary.txt").write_line("met a girl");
}
if error.is_some() {
println("failed to write my diary");
}
`File::create(&Path::new("diary.txt")).write(bytes!("Met a girl.\n"))`
without having to worry about whether "diary.txt" exists or whether
the write succeeds. As written, if either `new` or `write_line`
encounters an error the task will fail.
If you wanted to handle the error though you might write:
```rust
use std::io::File;
use std::io::{IoError, io_error};
let mut error = None;
io_error::cond.trap(|e: IoError| {
error = Some(e);
}).inside(|| {
File::create(&Path::new("diary.txt")).write(bytes!("Met a girl.\n"));
});
if error.is_some() {
println("failed to write my diary");
}
# ::std::io::fs::unlink(&Path::new("diary.txt"));
```
XXX: Need better condition handling syntax
Expand Down Expand Up @@ -498,10 +504,16 @@ pub trait Reader {
///
/// # Example
///
/// let mut reader = BufferedReader::new(File::open(&Path::new("foo.txt")));
/// for line in reader.lines() {
/// println(line);
/// }
/// ```rust
/// use std::io;
/// # let _g = ::std::io::ignore_io_error();
/// let mut reader = io::stdin();
///
/// let mut bytes = [0, .. 10];
/// reader.read(bytes);
///
/// if reader.eof() { println("stdin() had at most 10 bytes of data."); }
/// ```
///
/// # Failure
///
Expand Down Expand Up @@ -1057,6 +1069,18 @@ pub trait Buffer: Reader {
/// encoded unicode codepoints. If a newline is encountered, then the
/// newline is contained in the returned string.
///
/// # Example
///
/// ```rust
/// use std::io::buffered::BufferedReader;
/// use std::io;
/// # let _g = ::std::io::ignore_io_error();
///
/// let mut reader = BufferedReader::new(io::stdin());
///
/// let input = reader.read_line().unwrap_or(~"nothing");
/// ```
///
/// # Failure
///
/// This function will raise on the `io_error` condition (except for
Expand Down

5 comments on commit 95ace50

@bors
Copy link
Contributor

@bors bors commented on 95ace50 Jan 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from huonw
at adrientetar@95ace50

@bors
Copy link
Contributor

@bors bors commented on 95ace50 Jan 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging adridu59/rust/patch-io = 95ace50 into auto

@bors
Copy link
Contributor

@bors bors commented on 95ace50 Jan 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adridu59/rust/patch-io = 95ace50 merged ok, testing candidate = 14c24ac

@bors
Copy link
Contributor

@bors bors commented on 95ace50 Jan 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 95ace50 Jan 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 14c24ac

Please sign in to comment.