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<S: Str> Equiv<S> for str

fn equiv(&self, other: &S) -> bool

impl Slice<uint, str> for str

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

fn slice_from_or_fail(&'a self, from: &uint) -> &'a str

fn slice_to_or_fail(&'a self, to: &uint) -> &'a str

fn slice_or_fail(&'a self, from: &uint, to: &uint) -> &'a 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: CharEq>(&self, pat: P) -> bool

fn chars(&self) -> Chars

fn bytes(&self) -> Bytes

fn char_indices(&self) -> CharIndices

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

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

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

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

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) -> uint

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

fn as_ptr(&self) -> *const u8

fn len(&self) -> uint

fn is_empty(&self) -> bool

impl<'a> Default for &'a str

fn default() -> &'a str

impl<S: Writer> Hash<S> for str

fn hash(&self, state: &mut S)

impl Show for str

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

impl<'a> IntoMaybeOwned<'a> for &'a str

fn into_maybe_owned(self) -> MaybeOwned<'a>

Examples

fn main() { let string = "orange"; let maybe_owned_str = string.as_slice().into_maybe_owned(); assert_eq!(false, maybe_owned_str.is_owned()); }
let string = "orange";
let maybe_owned_str = string.as_slice().into_maybe_owned();
assert_eq!(false, maybe_owned_str.is_owned());

impl BorrowFrom<String> for str

fn borrow_from(owned: &String) -> &str

impl ToOwned<String> for str

fn to_owned(&self) -> String

impl StrExt for str

fn escape_default(&self) -> String

fn escape_unicode(&self) -> String

fn replace(&self, from: &str, to: &str) -> String

fn repeat(&self, nn: uint) -> String

fn lev_distance(&self, t: &str) -> uint

fn nfd_chars<'a>(&'a self) -> Decompositions<'a>

fn nfkd_chars<'a>(&'a self) -> Decompositions<'a>

fn nfc_chars<'a>(&'a self) -> Recompositions<'a>

fn nfkc_chars<'a>(&'a self) -> Recompositions<'a>

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

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

fn chars(&self) -> Chars

fn bytes(&self) -> Bytes

fn char_indices(&self) -> CharIndices

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

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

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

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

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

fn split_str<'a>(&'a self, pat: &'a str) -> StrSplits<'a>

fn lines(&self) -> Lines

fn lines_any(&self) -> LinesAny

fn char_len(&self) -> uint

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

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

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

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

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

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

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

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

fn trim_chars<'a, C: CharEq>(&'a self, to_trim: C) -> &'a str

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

fn trim_left_chars<'a, C: CharEq>(&'a self, to_trim: C) -> &'a str

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

fn trim_right_chars<'a, C: CharEq>(&'a self, to_trim: C) -> &'a str

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

fn char_range_at(&self, start: uint) -> CharRange

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

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

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

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

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

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

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

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

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

fn as_ptr(&self) -> *const u8

fn utf16_units(&self) -> Utf16Units

fn len(&self) -> uint

fn is_empty(&self) -> bool

fn parse<F: FromStr>(&self) -> Option<F>

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) -> uint

fn trim(&self) -> &str

fn trim_left(&self) -> &str

fn trim_right(&self) -> &str

fn into_string(&self) -> String

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) -> uint

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

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

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

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

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

fn into_cow(self) -> CowString<'a>