Skip to content

Commit da3e64d

Browse files
committed
std: io: add some code examples
Closes #11232.
1 parent a9c03e0 commit da3e64d

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/libstd/io/mod.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ an error the task will fail.
194194
If you wanted to handle the error though you might write
195195
196196
let mut error = None;
197-
do io_error::cond(|e: IoError| {
197+
do io_error::cond.trap(|e: IoError| {
198198
error = Some(e);
199199
}).in {
200200
File::new("diary.txt").write_line("met a girl");
@@ -498,10 +498,16 @@ pub trait Reader {
498498
///
499499
/// # Example
500500
///
501-
/// let mut reader = BufferedReader::new(File::open(&Path::new("foo.txt")));
502-
/// for line in reader.lines() {
503-
/// println(line);
504-
/// }
501+
/// ```rust
502+
/// use std::io::buffered::BufferedReader;
503+
/// use std::io::File;
504+
/// # let _g = ::std::io::ignore_io_error();
505+
///
506+
/// let mut reader = BufferedReader::new(File::open(&Path::new("foo.txt")));
507+
/// println(reader.read_line().unwrap_or(~"Expected at least one line."));
508+
///
509+
/// if reader.eof() { println("The file is only one line long."); }
510+
/// ```
505511
///
506512
/// # Failure
507513
///
@@ -1057,6 +1063,18 @@ pub trait Buffer: Reader {
10571063
/// encoded unicode codepoints. If a newline is encountered, then the
10581064
/// newline is contained in the returned string.
10591065
///
1066+
/// # Example
1067+
///
1068+
/// ```rust
1069+
/// use std::io::buffered::BufferedReader;
1070+
/// use std::io::stdin;
1071+
/// # let _g = ::std::io::ignore_io_error();
1072+
///
1073+
/// let mut reader = BufferedReader::new(std::io::stdin());
1074+
///
1075+
/// let input = reader.read_line().unwrap_or(~"nothing");
1076+
/// ```
1077+
///
10601078
/// # Failure
10611079
///
10621080
/// This function will raise on the `io_error` condition (except for

0 commit comments

Comments
 (0)