Skip to content

Add examples for some StrSlice methods. #16618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 20, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
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