Module collections::str [] [src]

Unicode string manipulation (the str`str` type).

Rust's str`strtype is one of the core primitive types of the language.` type is one of the core primitive types of the language. &str`&stris the borrowed string type. This type of string can only be created from other strings, unless it is a` is the borrowed string type. This type of string can only be created from other strings, unless it is a &'static str`&'static str` (see below). It is not possible to move out of borrowed strings because they are owned elsewhere.

Examples

Here's some code that uses a &str`&str`:

fn main() { let s = "Hello, world."; }
let s = "Hello, world.";

This &str`&stris a` is a &'static str`&'static str, which is the type of string literals. They're`, which is the type of string literals. They're 'static`'static` because literals are available for the entire lifetime of the program.

You can get a non-'static`'static` &str`&strby taking a slice of a` by taking a slice of a String`String`:

fn main() { let some_string = "Hello, world.".to_string(); let s = &some_string; }
let s = &some_string;

Representation

Rust's string type, str`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 str`strs have direct mappings to slices:`s have direct mappings to slices: &str`&stris the same as` is the same as &[u8]`&[u8]`.

Modules

pattern [Unstable]

The string Pattern API.

Structs

Bytes

External iterator for a string's bytes. Use with the std::iter`std::iter` module.

CharIndices

Iterator for a string's characters and their byte offsets.

Chars

Iterator for the char (representing Unicode Scalar Values) of a string

Lines

Created with the method .lines()`.lines()`.

LinesAny

Created with the method .lines_any()`.lines_any()`.

ParseBoolError

An error returned when parsing a bool`bool` from a string fails.

RSplit

/// Created with the method .rsplit()`.rsplit()`.

RSplitN

/// Created with the method .rsplitn()`.rsplitn()`.

RSplitTerminator

/// Created with the method .rsplit_terminator()`.rsplit_terminator()`.

Split

/// Created with the method .split()`.split()`.

SplitN

/// Created with the method .splitn()`.splitn()`.

SplitTerminator

/// Created with the method .split_terminator()`.split_terminator()`.

SplitWhitespace

An iterator over the non-whitespace substrings of a string, separated by any amount of whitespace.

Utf8Error

Errors which can occur when attempting to interpret a byte slice as a str`str`.

CharRange [Unstable]

Struct that contains a char`charand the index of the first byte of the next` and the index of the first byte of the next char`char` in a string. This can be used as a data structure for iterating over the UTF-8 bytes of a string.

Decompositions [Deprecated]

External iterator for a string decomposition's characters.

GraphemeIndices [Unstable]

External iterator for grapheme clusters and byte offsets.

Graphemes [Unstable]

External iterator for a string's grapheme clusters.

MatchIndices [Unstable]

/// Created with the method .match_indices()`.match_indices()`.

Matches [Unstable]

/// Created with the method .matches()`.matches()`.

RMatchIndices [Unstable]

/// Created with the method .rmatch_indices()`.rmatch_indices()`.

RMatches [Unstable]

/// Created with the method .rmatches()`.rmatches()`.

Recompositions [Deprecated]

External iterator for a string recomposition's characters.

Utf16Units [Unstable]

External iterator for a string's UTF16 codeunits.

Traits

FromStr

A trait to abstract the idea of creating a new instance of a type from a string.

Functions

from_utf8

Converts a slice of bytes to a string slice without performing any allocations.

from_utf8_unchecked

Converts a slice of bytes to a string slice without checking that the string contains valid UTF-8.

Type Definitions

Words [Deprecated]