@@ -1290,28 +1290,42 @@ pub trait BufRead: Read {
1290
1290
/// If an I/O error is encountered then all bytes read so far will be
1291
1291
/// present in `buf` and its length will have been adjusted appropriately.
1292
1292
///
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
- ///
1298
1293
/// [`fill_buf`]: #tymethod.fill_buf
1299
1294
/// [`ErrorKind::Interrupted`]: enum.ErrorKind.html#variant.Interrupted
1300
1295
///
1301
- /// ```
1302
- /// use std::io;
1303
- /// use std::io::prelude::*;
1296
+ /// # Examples
1304
1297
///
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:
1309
1301
///
1310
- /// stdin.read_until(b'a', &mut buffer)?;
1302
+ /// [`Cursor`]: struct.Cursor.html
1311
1303
///
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"");
1315
1329
/// ```
1316
1330
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1317
1331
fn read_until ( & mut self , byte : u8 , buf : & mut Vec < u8 > ) -> Result < usize > {
@@ -1337,28 +1351,36 @@ pub trait BufRead: Read {
1337
1351
///
1338
1352
/// # Examples
1339
1353
///
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:
1344
1356
///
1345
- /// [`lines`]: #method.lines
1346
- /// [`read_until`]: #method.read_until
1357
+ /// [`Cursor`]: struct.Cursor.html
1347
1358
///
1348
1359
/// ```
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, "");
1362
1384
/// ```
1363
1385
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1364
1386
fn read_line ( & mut self , buf : & mut String ) -> Result < usize > {
@@ -1378,24 +1400,28 @@ pub trait BufRead: Read {
1378
1400
/// This function will yield errors whenever [`read_until`] would have
1379
1401
/// also yielded an error.
1380
1402
///
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
- ///
1386
1403
/// [`io::Result`]: type.Result.html
1387
1404
/// [`Vec<u8>`]: ../vec/struct.Vec.html
1388
1405
/// [`read_until`]: #method.read_until
1389
1406
///
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
+ ///
1390
1415
/// ```
1391
- /// use std::io;
1392
- /// use std::io::prelude::*;
1416
+ /// use std::io::{self, BufRead};
1393
1417
///
1394
- /// let stdin = io::stdin( );
1418
+ /// let cursor = io::Cursor::new(b"lorem-ipsum-dolor" );
1395
1419
///
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);
1399
1425
/// ```
1400
1426
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1401
1427
fn split ( self , byte : u8 ) -> Split < Self > where Self : Sized {
@@ -1413,17 +1439,22 @@ pub trait BufRead: Read {
1413
1439
///
1414
1440
/// # Examples
1415
1441
///
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
1417
1447
///
1418
1448
/// ```
1419
- /// use std::io;
1420
- /// use std::io::prelude::*;
1449
+ /// use std::io::{self, BufRead};
1421
1450
///
1422
- /// let stdin = io::stdin( );
1451
+ /// let cursor = io::Cursor::new(b"lorem\nipsum\r\ndolor" );
1423
1452
///
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);
1427
1458
/// ```
1428
1459
///
1429
1460
/// # Errors
0 commit comments