Skip to content

Commit b22e840

Browse files
committed
auto merge of #16618 : nham/rust/strslice_examples, r=alexcrichton
(By chance, the `contains` example was actually what I was trying to write when I discovered #16589)
2 parents 05df25f + 0821119 commit b22e840

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/libcore/str.rs

+46
Original file line numberDiff line numberDiff line change
@@ -1094,13 +1094,25 @@ pub trait StrSlice<'a> {
10941094
/// # Arguments
10951095
///
10961096
/// - needle - The string to look for
1097+
///
1098+
/// # Example
1099+
///
1100+
/// ```rust
1101+
/// assert!("bananas".contains("nana"));
1102+
/// ```
10971103
fn contains<'a>(&self, needle: &'a str) -> bool;
10981104

10991105
/// Returns true if a string contains a char.
11001106
///
11011107
/// # Arguments
11021108
///
11031109
/// - needle - The char to look for
1110+
///
1111+
/// # Example
1112+
///
1113+
/// ```rust
1114+
/// assert!("hello".contains_char('e'));
1115+
/// ```
11041116
fn contains_char(&self, needle: char) -> bool;
11051117

11061118
/// An iterator over the characters of `self`. Note, this iterates
@@ -1115,6 +1127,13 @@ pub trait StrSlice<'a> {
11151127
fn chars(&self) -> Chars<'a>;
11161128

11171129
/// An iterator over the bytes of `self`
1130+
///
1131+
/// # Example
1132+
///
1133+
/// ```rust
1134+
/// let v: Vec<u8> = "bors".bytes().collect();
1135+
/// assert_eq!(v, b"bors".to_vec());
1136+
/// ```
11181137
fn bytes(&self) -> Bytes<'a>;
11191138

11201139
/// An iterator over the characters of `self` and their byte offsets.
@@ -1381,9 +1400,21 @@ pub trait StrSlice<'a> {
13811400
fn slice_chars(&self, begin: uint, end: uint) -> &'a str;
13821401

13831402
/// Returns true if `needle` is a prefix of the string.
1403+
///
1404+
/// # Example
1405+
///
1406+
/// ```rust
1407+
/// assert!("banana".starts_with("ba"));
1408+
/// ```
13841409
fn starts_with(&self, needle: &str) -> bool;
13851410

13861411
/// Returns true if `needle` is a suffix of the string.
1412+
///
1413+
/// # Example
1414+
///
1415+
/// ```rust
1416+
/// assert!("banana".ends_with("nana"));
1417+
/// ```
13871418
fn ends_with(&self, needle: &str) -> bool;
13881419

13891420
/// Returns a string with characters that match `to_trim` removed.
@@ -1525,6 +1556,15 @@ pub trait StrSlice<'a> {
15251556

15261557
/// Plucks the character starting at the `i`th byte of a string.
15271558
///
1559+
/// # Example
1560+
///
1561+
/// ```rust
1562+
/// let s = "abπc";
1563+
/// assert_eq!(s.char_at(1), 'b');
1564+
/// assert_eq!(s.char_at(2), 'π');
1565+
/// assert_eq!(s.char_at(4), 'c');
1566+
/// ```
1567+
///
15281568
/// # Failure
15291569
///
15301570
/// If `i` is greater than or equal to the length of the string.
@@ -1540,6 +1580,12 @@ pub trait StrSlice<'a> {
15401580
fn char_at_reverse(&self, i: uint) -> char;
15411581

15421582
/// Work with the byte buffer of a string as a byte slice.
1583+
///
1584+
/// # Example
1585+
///
1586+
/// ```rust
1587+
/// assert_eq!("bors".as_bytes(), b"bors");
1588+
/// ```
15431589
fn as_bytes(&self) -> &'a [u8];
15441590

15451591
/// Returns the byte index of the first character of `self` that

0 commit comments

Comments
 (0)