Skip to content

Commit

Permalink
Add examples for some StrSlice methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
nham committed Aug 19, 2014
1 parent 3f5d0b5 commit 0821119
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/libcore/str.rs
Expand Up @@ -1094,13 +1094,25 @@ pub trait StrSlice<'a> {
/// # Arguments
///
/// - needle - The string to look for
///
/// # Example
///
/// ```rust
/// assert!("bananas".contains("nana"));
/// ```
fn contains<'a>(&self, needle: &'a str) -> bool;

/// Returns true if a string contains a char.
///
/// # Arguments
///
/// - needle - The char to look for
///
/// # Example
///
/// ```rust
/// assert!("hello".contains_char('e'));
/// ```
fn contains_char(&self, needle: char) -> bool;

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

/// An iterator over the bytes of `self`
///
/// # Example
///
/// ```rust
/// let v: Vec<u8> = "bors".bytes().collect();
/// assert_eq!(v, b"bors".to_vec());
/// ```
fn bytes(&self) -> Bytes<'a>;

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

/// Returns true if `needle` is a prefix of the string.
///
/// # Example
///
/// ```rust
/// assert!("banana".starts_with("ba"));
/// ```
fn starts_with(&self, needle: &str) -> bool;

/// Returns true if `needle` is a suffix of the string.
///
/// # Example
///
/// ```rust
/// assert!("banana".ends_with("nana"));
/// ```
fn ends_with(&self, needle: &str) -> bool;

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

/// Plucks the character starting at the `i`th byte of a string.
///
/// # Example
///
/// ```rust
/// let s = "abπc";
/// assert_eq!(s.char_at(1), 'b');
/// assert_eq!(s.char_at(2), 'π');
/// assert_eq!(s.char_at(4), 'c');
/// ```
///
/// # Failure
///
/// If `i` is greater than or equal to the length of the string.
Expand All @@ -1540,6 +1580,12 @@ pub trait StrSlice<'a> {
fn char_at_reverse(&self, i: uint) -> char;

/// Work with the byte buffer of a string as a byte slice.
///
/// # Example
///
/// ```rust
/// assert_eq!("bors".as_bytes(), b"bors");
/// ```
fn as_bytes(&self) -> &'a [u8];

/// Returns the byte index of the first character of `self` that
Expand Down

5 comments on commit 0821119

@bors
Copy link
Contributor

@bors bors commented on 0821119 Aug 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at nham@0821119

@bors
Copy link
Contributor

@bors bors commented on 0821119 Aug 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging nham/rust/strslice_examples = 0821119 into auto

@bors
Copy link
Contributor

@bors bors commented on 0821119 Aug 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nham/rust/strslice_examples = 0821119 merged ok, testing candidate = b22e840

@bors
Copy link
Contributor

@bors bors commented on 0821119 Aug 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = b22e840

Please sign in to comment.