Skip to content

Commit 95ace50

Browse files
committed
std: io: add some code examples
Closes #11232.
1 parent ba80157 commit 95ace50

File tree

1 file changed

+45
-21
lines changed

1 file changed

+45
-21
lines changed

src/libstd/io/mod.rs

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -186,23 +186,29 @@ while still providing feedback about errors. The basic strategy:
186186
so that nullable values do not have to be 'unwrapped' before use.
187187
188188
These features combine in the API to allow for expressions like
189-
`File::new("diary.txt").write_line("met a girl")` without having to
190-
worry about whether "diary.txt" exists or whether the write
191-
succeeds. As written, if either `new` or `write_line` encounters
192-
an error the task will fail.
193-
194-
If you wanted to handle the error though you might write
195-
196-
let mut error = None;
197-
do io_error::cond(|e: IoError| {
198-
error = Some(e);
199-
}).in {
200-
File::new("diary.txt").write_line("met a girl");
201-
}
202-
203-
if error.is_some() {
204-
println("failed to write my diary");
205-
}
189+
`File::create(&Path::new("diary.txt")).write(bytes!("Met a girl.\n"))`
190+
without having to worry about whether "diary.txt" exists or whether
191+
the write succeeds. As written, if either `new` or `write_line`
192+
encounters an error the task will fail.
193+
194+
If you wanted to handle the error though you might write:
195+
196+
```rust
197+
use std::io::File;
198+
use std::io::{IoError, io_error};
199+
200+
let mut error = None;
201+
io_error::cond.trap(|e: IoError| {
202+
error = Some(e);
203+
}).inside(|| {
204+
File::create(&Path::new("diary.txt")).write(bytes!("Met a girl.\n"));
205+
});
206+
207+
if error.is_some() {
208+
println("failed to write my diary");
209+
}
210+
# ::std::io::fs::unlink(&Path::new("diary.txt"));
211+
```
206212
207213
XXX: Need better condition handling syntax
208214
@@ -498,10 +504,16 @@ pub trait Reader {
498504
///
499505
/// # Example
500506
///
501-
/// let mut reader = BufferedReader::new(File::open(&Path::new("foo.txt")));
502-
/// for line in reader.lines() {
503-
/// println(line);
504-
/// }
507+
/// ```rust
508+
/// use std::io;
509+
/// # let _g = ::std::io::ignore_io_error();
510+
/// let mut reader = io::stdin();
511+
///
512+
/// let mut bytes = [0, .. 10];
513+
/// reader.read(bytes);
514+
///
515+
/// if reader.eof() { println("stdin() had at most 10 bytes of data."); }
516+
/// ```
505517
///
506518
/// # Failure
507519
///
@@ -1057,6 +1069,18 @@ pub trait Buffer: Reader {
10571069
/// encoded unicode codepoints. If a newline is encountered, then the
10581070
/// newline is contained in the returned string.
10591071
///
1072+
/// # Example
1073+
///
1074+
/// ```rust
1075+
/// use std::io::buffered::BufferedReader;
1076+
/// use std::io;
1077+
/// # let _g = ::std::io::ignore_io_error();
1078+
///
1079+
/// let mut reader = BufferedReader::new(io::stdin());
1080+
///
1081+
/// let input = reader.read_line().unwrap_or(~"nothing");
1082+
/// ```
1083+
///
10601084
/// # Failure
10611085
///
10621086
/// This function will raise on the `io_error` condition (except for

0 commit comments

Comments
 (0)