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 see that Rust's string literals have the 'static lifetime. This is akin to C's concept of a static string.

String literals are allocated statically in the rodata of the executable/library. The string then has the type &'static str meaning that the string is valid for the 'static lifetime, otherwise known as the lifetime of the entire program. As can be inferred from the type, these static strings are not mutable.

Mutability

Many languages have immutable strings by default, and Rust has a particular flavor on this idea. As with the rest of Rust types, strings are immutable by default. If a string is declared as mut, however, it may be mutated. This works the same way as the rest of Rust's type system in the sense that if there's a mutable reference to a string, there may only be one mutable reference to that string. With these guarantees, strings can easily transition between being mutable/immutable with the same benefits of having mutable strings in other languages.

Representation

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

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

Trait Implementations

impl<'a> Clone for &'a str

fn clone<'a>(&self) -> &'a str

Return a shallow copy of the slice.

fn clone_from<'a>(&mut self, &&'a str)

impl<'a> Repr<Slice<u8>> for &'a str

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

impl<'a> Ord for &'a str

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

impl<'a> PartialEq for &'a str

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

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

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

impl<'a> Eq for &'a str

fn assert_receiver_is_total_eq<'a>(&self)

impl<'a> PartialOrd for &'a str

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

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

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

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

impl<'a, S: Str> Equiv<S> for &'a str

fn equiv<'a, S: Str>(&self, other: &S) -> bool

impl<'a> Str for &'a str

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

impl<'a> Collection for &'a str

fn len<'a>(&self) -> uint

fn is_empty<'a>(&self) -> bool

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

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

fn contains_char<'a>(&self, needle: char) -> bool

fn chars<'a>(&self) -> Chars<'a>

fn bytes<'a>(&self) -> Map<'a, &'a u8, u8, Items<'a, u8>>

fn char_indices<'a>(&self) -> CharOffsets<'a>

fn split<Sep: CharEq>(&self, sep: Sep) -> CharSplits<'a, Sep>

fn splitn<Sep: CharEq>(&self, sep: Sep, count: uint) -> CharSplitsN<'a, Sep>

fn split_terminator<Sep: CharEq>(&self, sep: Sep) -> CharSplits<'a, Sep>

fn rsplitn<Sep: CharEq>(&self, sep: Sep, count: uint) -> CharSplitsN<'a, Sep>

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

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

fn lines<'a>(&self) -> CharSplits<'a, char>

fn lines_any<'a>(&self) -> Map<'a, &'a str, &'a str, CharSplits<'a, char>>

fn words<'a>(&self) -> Filter<'a, &'a str, CharSplits<'a, fn(char) -> bool>>

fn is_whitespace<'a>(&self) -> bool

fn is_alphanumeric<'a>(&self) -> bool

fn char_len<'a>(&self) -> uint

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

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

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

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

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

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

fn trim<'a>(&self) -> &'a str

fn trim_left<'a>(&self) -> &'a str

fn trim_right<'a>(&self) -> &'a str

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

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

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

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

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

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

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

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

fn as_bytes<'a>(&self) -> &'a [u8]

fn find<C: CharEq>(&self, search: C) -> Option<uint>

fn rfind<C: CharEq>(&self, search: C) -> Option<uint>

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

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

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

fn as_ptr<'a>(&self) -> *u8

impl<'a> Default for &'a str

fn default<'a>() -> &'a str

impl<'a> Show for &'a str

fn fmt<'a>(&self, f: &mut Formatter) -> Result<(), FormatError>

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

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

impl<'a> StrAllocating for &'a str

fn into_string(self) -> String

fn to_string(&self) -> String

fn into_owned(self) -> String

fn escape_default(&self) -> String

fn escape_unicode(&self) -> String

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

fn to_owned(&self) -> String

fn to_utf16(&self) -> Vec<u16>

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>

impl<'a, S: Writer> Hash<S> for &'a str

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