Primitive Type str [-] [+]

Unicode string manipulation (str type)

Basic Usage

Rust's string type is one of the core primitive types of the language. While represented by the name str, the name str is not actually a valid type in Rust. Each string must also be decorated with a pointer. String is used for an owned string, so there is only one commonly-used str type in Rust: &str.

&str is the borrowed string type. This type of string can only be created from other strings, unless it is a static string (see below). As the word "borrowed" implies, this type of string is owned elsewhere, and this string cannot be moved out of.

As an example, here's some code that uses a string.

fn main() { let borrowed_string = "This string is borrowed with the 'static lifetime"; }
fn main() {
    let borrowed_string = "This string is borrowed with the 'static lifetime";
}

From the example above, you can guess that Rust's string literals have the 'static lifetime. This is akin to C's concept of a static string. More precisely, string literals are immutable views with a 'static lifetime (otherwise known as the lifetime of the entire program), and thus have the type &'static str.

Representation

Rust's string type, str, is a sequence of Unicode scalar values encoded as a stream of UTF-8 bytes. All strings are guaranteed to be validly encoded UTF-8 sequences. Additionally, strings are not null-terminated and can thus contain null bytes.

The actual representation of strings have direct mappings to slices: &str is the same as &[u8].

Trait Implementations

impl Repr<Slice<u8>> for str

fn repr(&self) -> Slice<u8>

impl Ord for str

fn cmp(&self, other: &str) -> Ordering

impl PartialEq<str> for str

fn eq(&self, other: &str) -> bool

fn ne(&self, other: &str) -> bool

fn ne(&self, &str) -> bool

impl Eq for str

fn assert_receiver_is_total_eq(&self)

impl PartialOrd<str> for str

fn partial_cmp(&self, other: &str) -> Option<Ordering>

fn lt(&self, &str) -> bool

fn le(&self, &str) -> bool

fn gt(&self, &str) -> bool

fn ge(&self, &str) -> bool

impl Index<Range<usize>> for str

Returns a slice of the given string from the byte range [begin..end).

This operation is O(1).

Panics when begin and end do not point to valid characters or point beyond the last character of the string.

Example

fn main() { let s = "Löwe 老虎 Léopard"; assert_eq!(&s[0 .. 1], "L"); assert_eq!(&s[1 .. 9], "öwe 老"); // these will panic: // byte 2 lies within `ö`: // &s[2 ..3]; // byte 8 lies within `老` // &s[1 .. 8]; // byte 100 is outside the string // &s[3 .. 100]; }
let s = "Löwe 老虎 Léopard";
assert_eq!(&s[0 .. 1], "L");

assert_eq!(&s[1 .. 9], "öwe 老");

// these will panic:
// byte 2 lies within `ö`:
// &s[2 ..3];

// byte 8 lies within `老`
// &s[1 .. 8];

// byte 100 is outside the string
// &s[3 .. 100];

type Output = str

fn index(&self, index: &Range<usize>) -> &str

impl Index<RangeTo<usize>> for str

Returns a slice of the string from the beginning to byte end.

Equivalent to self[0 .. end].

Panics when end does not point to a valid character, or is out of bounds.

type Output = str

fn index(&self, index: &RangeTo<usize>) -> &str

impl Index<RangeFrom<usize>> for str

Returns a slice of the string from begin to its end.

Equivalent to self[begin .. self.len()].

Panics when begin does not point to a valid character, or is out of bounds.

type Output = str

fn index(&self, index: &RangeFrom<usize>) -> &str

impl Index<RangeFull> for str

type Output = str

fn index(&self, _index: &RangeFull) -> &str

impl Str for str

fn as_slice(&'a self) -> &'a str

impl StrExt for str

fn contains(&self, needle: &str) -> bool

fn contains_char<P>(&self, pat: P) -> bool where P: CharEq

fn chars(&self) -> Chars

fn bytes(&self) -> Bytes

fn char_indices(&self) -> CharIndices

fn split<P>(&self, pat: P) -> Split<P> where P: CharEq

fn splitn<P>(&self, count: usize, pat: P) -> SplitN<P> where P: CharEq

fn split_terminator<P>(&self, pat: P) -> SplitTerminator<P> where P: CharEq

fn rsplitn<P>(&self, count: usize, pat: P) -> RSplitN<P> where P: CharEq

fn match_indices(&'a self, sep: &'a str) -> MatchIndices<'a>

fn split_str(&'a self, sep: &'a str) -> SplitStr<'a>

fn lines(&self) -> Lines

fn lines_any(&self) -> LinesAny

fn char_len(&self) -> usize

fn slice_chars(&self, begin: usize, end: usize) -> &str

unsafe fn slice_unchecked(&self, begin: usize, end: usize) -> &str

fn starts_with(&self, needle: &str) -> bool

fn ends_with(&self, needle: &str) -> bool

fn trim_matches<P>(&self, pat: P) -> &str where P: CharEq

fn trim_left_matches<P>(&self, pat: P) -> &str where P: CharEq

fn trim_right_matches<P>(&self, pat: P) -> &str where P: CharEq

fn is_char_boundary(&self, index: usize) -> bool

fn char_range_at(&self, i: usize) -> CharRange

fn char_range_at_reverse(&self, start: usize) -> CharRange

fn char_at(&self, i: usize) -> char

fn char_at_reverse(&self, i: usize) -> char

fn as_bytes(&self) -> &[u8]

fn find<P>(&self, pat: P) -> Option<usize> where P: CharEq

fn rfind<P>(&self, pat: P) -> Option<usize> where P: CharEq

fn find_str(&self, needle: &str) -> Option<usize>

fn slice_shift_char(&self) -> Option<(char, &str)>

fn subslice_offset(&self, inner: &str) -> usize

fn as_ptr(&self) -> *const u8

fn len(&self) -> usize

fn is_empty(&self) -> bool

fn parse<T>(&self) -> Result<T, <T as FromStr>::Err> where T: FromStr

impl<'a> Default for &'a str

fn default() -> &'a str

impl Hash for str

fn hash<H>(&self, state: &mut H) where H: Hasher

fn hash_slice<H>(&[str], &mut H) where H: Hasher, str: Sized

impl Debug for str

fn fmt(&self, f: &mut Formatter) -> Result<(), Error>

impl Display for str

fn fmt(&self, f: &mut Formatter) -> Result<(), Error>

impl ToOwned for str

type Owned = String

fn to_owned(&self) -> String

impl StrExt for str

fn slice(&self, begin: usize, end: usize) -> &str

fn slice_from(&self, begin: usize) -> &str

fn slice_to(&self, end: usize) -> &str

fn escape_default(&self) -> String

fn escape_unicode(&self) -> String

fn replace(&self, &str, &str) -> String

fn nfd_chars(&self) -> Decompositions

fn nfkd_chars(&self) -> Decompositions

fn nfc_chars(&self) -> Recompositions

fn nfkc_chars(&self) -> Recompositions

fn contains(&self, &str) -> bool

fn contains_char<P>(&self, P) -> bool where P: CharEq

fn chars(&self) -> Chars

fn bytes(&self) -> Bytes

fn char_indices(&self) -> CharIndices

fn split<P>(&self, P) -> Split<P> where P: CharEq

fn splitn<P>(&self, usize, P) -> SplitN<P> where P: CharEq

fn split_terminator<P>(&self, P) -> SplitTerminator<P> where P: CharEq

fn rsplitn<P>(&self, usize, P) -> RSplitN<P> where P: CharEq

fn match_indices(&'a self, &'a str) -> MatchIndices<'a>

fn split_str(&'a self, &'a str) -> SplitStr<'a>

fn lines(&self) -> Lines

fn lines_any(&self) -> LinesAny

fn slice_chars(&self, usize, usize) -> &str

unsafe fn slice_unchecked(&self, usize, usize) -> &str

fn starts_with(&self, &str) -> bool

fn ends_with(&self, &str) -> bool

fn trim_matches<P>(&self, P) -> &str where P: CharEq

fn trim_left_matches<P>(&self, P) -> &str where P: CharEq

fn trim_right_matches<P>(&self, P) -> &str where P: CharEq

fn is_char_boundary(&self, usize) -> bool

fn char_range_at(&self, usize) -> CharRange

fn char_range_at_reverse(&self, usize) -> CharRange

fn char_at(&self, usize) -> char

fn char_at_reverse(&self, usize) -> char

fn as_bytes(&self) -> &[u8]

fn find<P>(&self, P) -> Option<usize> where P: CharEq

fn rfind<P>(&self, P) -> Option<usize> where P: CharEq

fn find_str(&self, &str) -> Option<usize>

fn slice_shift_char(&self) -> Option<(char, &str)>

fn subslice_offset(&self, &str) -> usize

fn as_ptr(&self) -> *const u8

fn utf16_units(&self) -> Utf16Units

fn len(&self) -> usize

fn is_empty(&self) -> bool

fn parse<F>(&self) -> Result<F, <F as FromStr>::Err> where F: FromStr

fn graphemes(&self, bool) -> Graphemes

fn grapheme_indices(&self, bool) -> GraphemeIndices

fn words(&self) -> Words

fn width(&self, bool) -> usize

fn trim(&self) -> &str

fn trim_left(&self) -> &str

fn trim_right(&self) -> &str

impl<'a> PartialEq<String> for &'a str

fn eq(&self, other: &String) -> bool

fn ne(&self, other: &String) -> bool

fn ne(&self, &String) -> bool

impl<'a, 'b> PartialEq<Cow<'a, str>> for &'b str

fn eq(&self, other: &Cow<'a, str>) -> bool

fn ne(&self, other: &Cow<'a, str>) -> bool

fn ne(&self, &Cow<'a, str>) -> bool

impl<'a> IntoCow<'a, str> for &'a str

fn into_cow(self) -> Cow<'a, str>

impl UnicodeStr for str

fn graphemes(&self, is_extended: bool) -> Graphemes

fn grapheme_indices(&self, is_extended: bool) -> GraphemeIndices

fn words(&self) -> Words

fn is_whitespace(&self) -> bool

fn is_alphanumeric(&self) -> bool

fn width(&self, is_cjk: bool) -> usize

fn trim(&self) -> &str

fn trim_left(&self) -> &str

fn trim_right(&self) -> &str

impl AsciiExt for str

type Owned = String

fn is_ascii(&self) -> bool

fn to_ascii_uppercase(&self) -> String

fn to_ascii_lowercase(&self) -> String

fn eq_ignore_ascii_case(&self, other: &str) -> bool

fn make_ascii_uppercase(&mut self)

fn make_ascii_lowercase(&mut self)

impl<'a> IntoBytes for &'a str

fn into_bytes(self) -> Vec<u8>

impl PartialEq<OsString> for str

fn eq(&self, other: &OsString) -> bool

fn ne(&self, other: &Rhs) -> bool

impl PartialEq<OsStr> for str

fn eq(&self, other: &OsStr) -> bool

fn ne(&self, other: &Rhs) -> bool

impl AsOsStr for str

fn as_os_str(&self) -> &OsStr

impl<'a> ToSocketAddr for &'a str

fn to_socket_addr(&self) -> IoResult<SocketAddr>

fn to_socket_addr_all(&self) -> IoResult<Vec<SocketAddr>>

impl ToSocketAddrs for str

type Iter = IntoIter<SocketAddr>

fn to_socket_addrs(&self) -> Result<IntoIter<SocketAddr>>

impl BytesContainer for str

fn container_as_bytes(&self) -> &[u8]

fn container_as_str(&self) -> Option<&str>

fn is_str(_: Option<&str>) -> bool