@@ -1290,28 +1290,42 @@ pub trait BufRead: Read {
12901290 /// If an I/O error is encountered then all bytes read so far will be
12911291 /// present in `buf` and its length will have been adjusted appropriately.
12921292 ///
1293- /// # Examples
1294- ///
1295- /// A locked standard input implements `BufRead`. In this example, we'll
1296- /// read from standard input until we see an `a` byte.
1297- ///
12981293 /// [`fill_buf`]: #tymethod.fill_buf
12991294 /// [`ErrorKind::Interrupted`]: enum.ErrorKind.html#variant.Interrupted
13001295 ///
1301- /// ```
1302- /// use std::io;
1303- /// use std::io::prelude::*;
1296+ /// # Examples
13041297 ///
1305- /// fn foo() -> io::Result<()> {
1306- /// let stdin = io::stdin();
1307- /// let mut stdin = stdin.lock();
1308- /// let mut buffer = Vec::new();
1298+ /// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
1299+ /// this example, we use [`Cursor`] to read all the bytes in a byte slice
1300+ /// in hyphen delimited segments:
13091301 ///
1310- /// stdin.read_until(b'a', &mut buffer)?;
1302+ /// [`Cursor`]: struct.Cursor.html
13111303 ///
1312- /// println!("{:?}", buffer);
1313- /// # Ok(())
1314- /// # }
1304+ /// ```
1305+ /// use std::io::{self, BufRead};
1306+ ///
1307+ /// let mut cursor = io::Cursor::new(b"lorem-ipsum");
1308+ /// let mut buf = vec![];
1309+ ///
1310+ /// // cursor is at 'l'
1311+ /// let num_bytes = cursor.read_until(b'-', &mut buf)
1312+ /// .expect("reading from cursor won't fail");
1313+ /// assert_eq!(num_bytes, 6);
1314+ /// assert_eq!(buf, b"lorem-");
1315+ /// buf.clear();
1316+ ///
1317+ /// // cursor is at 'i'
1318+ /// let num_bytes = cursor.read_until(b'-', &mut buf)
1319+ /// .expect("reading from cursor won't fail");
1320+ /// assert_eq!(num_bytes, 5);
1321+ /// assert_eq!(buf, b"ipsum");
1322+ /// buf.clear();
1323+ ///
1324+ /// // cursor is at EOF
1325+ /// let num_bytes = cursor.read_until(b'-', &mut buf)
1326+ /// .expect("reading from cursor won't fail");
1327+ /// assert_eq!(num_bytes, 0);
1328+ /// assert_eq!(buf, b"");
13151329 /// ```
13161330 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
13171331 fn read_until ( & mut self , byte : u8 , buf : & mut Vec < u8 > ) -> Result < usize > {
@@ -1337,28 +1351,36 @@ pub trait BufRead: Read {
13371351 ///
13381352 /// # Examples
13391353 ///
1340- /// A locked standard input implements `BufRead`. In this example, we'll
1341- /// read all of the lines from standard input. If we were to do this in
1342- /// an actual project, the [`lines`] method would be easier, of
1343- /// course.
1354+ /// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
1355+ /// this example, we use [`Cursor`] to read all the lines in a byte slice:
13441356 ///
1345- /// [`lines`]: #method.lines
1346- /// [`read_until`]: #method.read_until
1357+ /// [`Cursor`]: struct.Cursor.html
13471358 ///
13481359 /// ```
1349- /// use std::io;
1350- /// use std::io::prelude::*;
1351- ///
1352- /// let stdin = io::stdin();
1353- /// let mut stdin = stdin.lock();
1354- /// let mut buffer = String::new();
1355- ///
1356- /// while stdin.read_line(&mut buffer).unwrap() > 0 {
1357- /// // work with buffer
1358- /// println!("{:?}", buffer);
1359- ///
1360- /// buffer.clear();
1361- /// }
1360+ /// use std::io::{self, BufRead};
1361+ ///
1362+ /// let mut cursor = io::Cursor::new(b"foo\nbar");
1363+ /// let mut buf = String::new();
1364+ ///
1365+ /// // cursor is at 'f'
1366+ /// let num_bytes = cursor.read_line(&mut buf)
1367+ /// .expect("reading from cursor won't fail");
1368+ /// assert_eq!(num_bytes, 4);
1369+ /// assert_eq!(buf, "foo\n");
1370+ /// buf.clear();
1371+ ///
1372+ /// // cursor is at 'b'
1373+ /// let num_bytes = cursor.read_line(&mut buf)
1374+ /// .expect("reading from cursor won't fail");
1375+ /// assert_eq!(num_bytes, 3);
1376+ /// assert_eq!(buf, "bar");
1377+ /// buf.clear();
1378+ ///
1379+ /// // cursor is at EOF
1380+ /// let num_bytes = cursor.read_line(&mut buf)
1381+ /// .expect("reading from cursor won't fail");
1382+ /// assert_eq!(num_bytes, 0);
1383+ /// assert_eq!(buf, "");
13621384 /// ```
13631385 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
13641386 fn read_line ( & mut self , buf : & mut String ) -> Result < usize > {
@@ -1378,24 +1400,28 @@ pub trait BufRead: Read {
13781400 /// This function will yield errors whenever [`read_until`] would have
13791401 /// also yielded an error.
13801402 ///
1381- /// # Examples
1382- ///
1383- /// A locked standard input implements `BufRead`. In this example, we'll
1384- /// read some input from standard input, splitting on commas.
1385- ///
13861403 /// [`io::Result`]: type.Result.html
13871404 /// [`Vec<u8>`]: ../vec/struct.Vec.html
13881405 /// [`read_until`]: #method.read_until
13891406 ///
1407+ /// # Examples
1408+ ///
1409+ /// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
1410+ /// this example, we use [`Cursor`] to iterate over all hyphen delimited
1411+ /// segments in a byte slice
1412+ ///
1413+ /// [`Cursor`]: struct.Cursor.html
1414+ ///
13901415 /// ```
1391- /// use std::io;
1392- /// use std::io::prelude::*;
1416+ /// use std::io::{self, BufRead};
13931417 ///
1394- /// let stdin = io::stdin( );
1418+ /// let cursor = io::Cursor::new(b"lorem-ipsum-dolor" );
13951419 ///
1396- /// for content in stdin.lock().split(b',') {
1397- /// println!("{:?}", content.unwrap());
1398- /// }
1420+ /// let mut split_iter = cursor.split(b'-').map(|l| l.unwrap());
1421+ /// assert_eq!(split_iter.next(), Some(b"lorem".to_vec()));
1422+ /// assert_eq!(split_iter.next(), Some(b"ipsum".to_vec()));
1423+ /// assert_eq!(split_iter.next(), Some(b"dolor".to_vec()));
1424+ /// assert_eq!(split_iter.next(), None);
13991425 /// ```
14001426 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
14011427 fn split ( self , byte : u8 ) -> Split < Self > where Self : Sized {
@@ -1413,17 +1439,22 @@ pub trait BufRead: Read {
14131439 ///
14141440 /// # Examples
14151441 ///
1416- /// A locked standard input implements `BufRead`:
1442+ /// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
1443+ /// this example, we use [`Cursor`] to iterate over all the lines in a byte
1444+ /// slice.
1445+ ///
1446+ /// [`Cursor`]: struct.Cursor.html
14171447 ///
14181448 /// ```
1419- /// use std::io;
1420- /// use std::io::prelude::*;
1449+ /// use std::io::{self, BufRead};
14211450 ///
1422- /// let stdin = io::stdin( );
1451+ /// let cursor = io::Cursor::new(b"lorem\nipsum\r\ndolor" );
14231452 ///
1424- /// for line in stdin.lock().lines() {
1425- /// println!("{}", line.unwrap());
1426- /// }
1453+ /// let mut lines_iter = cursor.lines().map(|l| l.unwrap());
1454+ /// assert_eq!(lines_iter.next(), Some(String::from("lorem")));
1455+ /// assert_eq!(lines_iter.next(), Some(String::from("ipsum")));
1456+ /// assert_eq!(lines_iter.next(), Some(String::from("dolor")));
1457+ /// assert_eq!(lines_iter.next(), None);
14271458 /// ```
14281459 ///
14291460 /// # Errors
0 commit comments