Module collections::str [] [src]

Unicode string manipulation (the str type).

Rust's str type is one of the core primitive types of the language. &str is the borrowed string type. This type of string can only be created from other strings, unless it is a &'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:

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

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

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

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

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 strs have direct mappings to slices: &str is the same as &[u8].

Modules

pattern [Unstable]

The string Pattern API.

Structs

Bytes

External iterator for a string's bytes. Use with the 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().

LinesAny

Created with the method .lines_any().

ParseBoolError

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

RSplit

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

RSplitN

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

RSplitTerminator

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

Split

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

SplitN

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

SplitTerminator

/// Created with the method .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.

CharRange [Unstable]

Struct that contains a char and the index of the first byte of the next 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().

Matches [Unstable]

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

RMatchIndices [Unstable]

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

RMatches [Unstable]

/// Created with the method .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]