@@ -31,7 +31,7 @@ Some examples of obvious things you might want to do
31
31
use std::io;
32
32
33
33
for line in io::stdin().lines() {
34
- print!("{}", line);
34
+ print!("{}", line.unwrap() );
35
35
}
36
36
```
37
37
@@ -57,26 +57,26 @@ Some examples of obvious things you might want to do
57
57
58
58
* Iterate over the lines of a file
59
59
60
- ```rust
60
+ ```rust,no_run
61
61
use std::io::BufferedReader;
62
62
use std::io::File;
63
63
64
64
let path = Path::new("message.txt");
65
65
let mut file = BufferedReader::new(File::open(&path));
66
66
for line in file.lines() {
67
- print!("{}", line);
67
+ print!("{}", line.unwrap() );
68
68
}
69
69
```
70
70
71
71
* Pull the lines of a file into a vector of strings
72
72
73
- ```rust
73
+ ```rust,no_run
74
74
use std::io::BufferedReader;
75
75
use std::io::File;
76
76
77
77
let path = Path::new("message.txt");
78
78
let mut file = BufferedReader::new(File::open(&path));
79
- let lines: ~[~str] = file.lines().collect();
79
+ let lines: ~[~str] = file.lines().map(|x| x.unwrap()). collect();
80
80
```
81
81
82
82
* Make a simple TCP client connection and request
@@ -466,10 +466,8 @@ pub trait Reader {
466
466
///
467
467
/// # Error
468
468
///
469
- /// The iterator protocol causes all specifics about errors encountered to
470
- /// be swallowed. All errors will be signified by returning `None` from the
471
- /// iterator. If this is undesirable, it is recommended to use the
472
- /// `read_byte` method.
469
+ /// Any error other than `EndOfFile` that is produced by the underlying Reader
470
+ /// is returned by the iterator and should be handled by the caller.
473
471
fn bytes < ' r > ( & ' r mut self ) -> extensions:: Bytes < ' r , Self > {
474
472
extensions:: Bytes :: new ( self )
475
473
}
@@ -986,7 +984,7 @@ pub trait Stream: Reader + Writer { }
986
984
impl < T : Reader + Writer > Stream for T { }
987
985
988
986
/// An iterator that reads a line on each iteration,
989
- /// until `.read_line()` returns `None `.
987
+ /// until `.read_line()` encounters `EndOfFile `.
990
988
///
991
989
/// # Notes about the Iteration Protocol
992
990
///
@@ -996,21 +994,24 @@ impl<T: Reader + Writer> Stream for T {}
996
994
///
997
995
/// # Error
998
996
///
999
- /// This iterator will swallow all I/O errors, transforming `Err` values to
1000
- /// `None`. If errors need to be handled, it is recommended to use the
1001
- /// `read_line` method directly.
997
+ /// Any error other than `EndOfFile` that is produced by the underlying Reader
998
+ /// is returned by the iterator and should be handled by the caller.
1002
999
pub struct Lines < ' r , T > {
1003
1000
priv buffer : & ' r mut T ,
1004
1001
}
1005
1002
1006
- impl < ' r , T : Buffer > Iterator < ~str > for Lines < ' r , T > {
1007
- fn next ( & mut self ) -> Option < ~str > {
1008
- self . buffer . read_line ( ) . ok ( )
1003
+ impl < ' r , T : Buffer > Iterator < IoResult < ~str > > for Lines < ' r , T > {
1004
+ fn next ( & mut self ) -> Option < IoResult < ~str > > {
1005
+ match self . buffer . read_line ( ) {
1006
+ Ok ( x) => Some ( Ok ( x) ) ,
1007
+ Err ( IoError { kind : EndOfFile , ..} ) => None ,
1008
+ Err ( y) => Some ( Err ( y) )
1009
+ }
1009
1010
}
1010
1011
}
1011
1012
1012
1013
/// An iterator that reads a utf8-encoded character on each iteration,
1013
- /// until `.read_char()` returns `None `.
1014
+ /// until `.read_char()` encounters `EndOfFile `.
1014
1015
///
1015
1016
/// # Notes about the Iteration Protocol
1016
1017
///
@@ -1020,16 +1021,19 @@ impl<'r, T: Buffer> Iterator<~str> for Lines<'r, T> {
1020
1021
///
1021
1022
/// # Error
1022
1023
///
1023
- /// This iterator will swallow all I/O errors, transforming `Err` values to
1024
- /// `None`. If errors need to be handled, it is recommended to use the
1025
- /// `read_char` method directly.
1024
+ /// Any error other than `EndOfFile` that is produced by the underlying Reader
1025
+ /// is returned by the iterator and should be handled by the caller.
1026
1026
pub struct Chars < ' r , T > {
1027
1027
priv buffer : & ' r mut T
1028
1028
}
1029
1029
1030
- impl < ' r , T : Buffer > Iterator < char > for Chars < ' r , T > {
1031
- fn next ( & mut self ) -> Option < char > {
1032
- self . buffer . read_char ( ) . ok ( )
1030
+ impl < ' r , T : Buffer > Iterator < IoResult < char > > for Chars < ' r , T > {
1031
+ fn next ( & mut self ) -> Option < IoResult < char > > {
1032
+ match self . buffer . read_char ( ) {
1033
+ Ok ( x) => Some ( Ok ( x) ) ,
1034
+ Err ( IoError { kind : EndOfFile , ..} ) => None ,
1035
+ Err ( y) => Some ( Err ( y) )
1036
+ }
1033
1037
}
1034
1038
}
1035
1039
@@ -1095,9 +1099,8 @@ pub trait Buffer: Reader {
1095
1099
///
1096
1100
/// # Error
1097
1101
///
1098
- /// This iterator will transform all error values to `None`, discarding the
1099
- /// cause of the error. If this is undesirable, it is recommended to call
1100
- /// `read_line` directly.
1102
+ /// Any error other than `EndOfFile` that is produced by the underlying Reader
1103
+ /// is returned by the iterator and should be handled by the caller.
1101
1104
fn lines < ' r > ( & ' r mut self ) -> Lines < ' r , Self > {
1102
1105
Lines { buffer : self }
1103
1106
}
@@ -1183,9 +1186,8 @@ pub trait Buffer: Reader {
1183
1186
///
1184
1187
/// # Error
1185
1188
///
1186
- /// This iterator will transform all error values to `None`, discarding the
1187
- /// cause of the error. If this is undesirable, it is recommended to call
1188
- /// `read_char` directly.
1189
+ /// Any error other than `EndOfFile` that is produced by the underlying Reader
1190
+ /// is returned by the iterator and should be handled by the caller.
1189
1191
fn chars < ' r > ( & ' r mut self ) -> Chars < ' r , Self > {
1190
1192
Chars { buffer : self }
1191
1193
}
0 commit comments