docs

core::str

本页内容

API 参考只有英文版,由标准库源代码中的英文文档注释生成。

str methods and trait impls.

str is an immutable (ptr, len) value: literals are static bytes, run-time strings GC objects, and a slice shares its source's bytes, so slicing, lines, split*, match_indices, trim* and strip_* copy nothing. StrBuf is the growable buffer.

Modules

  • unicode Unicode text on str: normalization forms, full case folding, and segmentation into grapheme clusters (user-perceived characters) and words.

Structs

CharsSource

pub struct Chars

What s.chars() returns: the Unicode scalar values, in order.

Trait implementations 2

SplitSource

pub struct Split

What s.split(sep) returns: the pieces between occurrences of sep, left to right.

Trait implementations 2

SplitNSource

pub struct SplitN

What s.splitn(n, sep) returns: at most n pieces, the last one the unsplit rest.

Trait implementations 2

RSplitSource

pub struct RSplit

What s.rsplit(sep) returns: the pieces of split, last first.

Trait implementations 2

RSplitNSource

pub struct RSplitN

What s.rsplitn(n, sep) returns: at most n pieces from the end, the last one the unsplit start.

Trait implementations 2

MatchIndicesSource

pub struct MatchIndices

What s.match_indices(pat) returns: (byte offset, match) pairs, left to right.

Trait implementations 2

LinesSource

pub struct Lines

What s.lines() returns: the lines without their \n / \r\n.

Trait implementations 2

CharIndicesSource

pub struct CharIndices

What s.char_indices() returns: (byte offset, char) pairs, in order.

Trait implementations 2

SplitWhitespaceSource

pub struct SplitWhitespace

What s.split_whitespace() returns: the non-empty pieces between runs of whitespace, left to right.

Trait implementations 2

StrBufSource

pub struct StrBuf

A growable UTF-8 buffer. A handle like the collections: copies share the buffer, one object holding the length, the capacity and the bytes.

  • pub const fn new() -> StrBuf§

    Source

    An empty buffer with room for 16 bytes.

  • pub const fn with_capacity(cap: usize) -> StrBuf§

    Source

    An empty buffer with room for cap bytes.

  • pub const fn len(self) -> usize§

    Source

    Byte length.

  • pub const fn is_empty(self) -> bool§

    Source

    true when the buffer holds no bytes.

  • pub const fn capacity(self) -> usize§

    Source

    Bytes the buffer holds before it grows.

  • pub const fn clear(self)§

    Source

    Empty the buffer; its capacity stays.

  • pub const fn reserve(self, additional: usize)§

    Source

    Room for additional more bytes (doubling).

  • pub const fn push(self, c: char)§

    Source

    Append c, UTF-8 encoded.

  • pub const fn push_str(self, s: str)§

    Source

    Append s, growing the buffer as needed.

  • pub const unsafe fn push_byte(self, b: u8)§

    Source

    Append one byte. unsafe: to_str hands out the bytes as a str, which must be UTF-8; the caller keeps them so by the time it is called. Bytes of unknown encoding go through str::from_utf8.

  • pub const fn to_str(self) -> str§

    Source

    The current contents as an immutable str (a copy).

Trait implementations 7

Utf8ErrorSource

pub struct Utf8Error

Bytes that are not UTF-8 (str::from_utf8, RawStr::to_str, RawPath::to_path; Windows paths: not UTF-16).

  • pub const fn valid_up_to(self) -> usize§

    Source

    Length of the valid prefix, in bytes (a Windows path's: UTF-16 units).

  • pub const fn message(self) -> str§

    Source

    What went wrong, for display.

Trait implementations 3
  • impl Display for Utf8Error
  • impl Debug for Utf8Error
  • impl Error for Utf8Error

Utf16ErrorSource

pub struct Utf16Error

UTF-16 with an unpaired surrogate (str::from_utf16, bare::ffi::WideCStr::to_str).

  • pub const fn valid_up_to(self) -> usize§

    Source

    Length of the valid prefix, in units.

  • pub const fn message(self) -> str§

    Source

    What went wrong, for display.

Trait implementations 3
  • impl Display for Utf16Error
  • impl Debug for Utf16Error
  • impl Error for Utf16Error

Traits

FromStrSource

pub trait FromStr

str.parse::<T>() dispatch.

Type aliases

BytesSource

pub type Bytes = SliceIter<u8>

What s.bytes() returns: the bytes of the as_bytes view.

Implementations

impl str

  • pub const fn new() -> str§

    Source

    The empty string.

  • pub const fn from_utf8<B: AsBytes>(bytes: B) throws (Utf8Error) -> str§

    Source

    bytes as a str; throws if they are not UTF-8.

  • pub const fn from_utf8_lossy<B: AsBytes>(bytes: B) -> str§

    Source

    bytes as a str with each ill-formed run replaced by U+FFFD, one per maximal subpart (Unicode 3.9 "U+FFFD Substitution of Maximal Subparts", as WHATWG UTF-8 decode does).

  • pub const fn encode_utf16(self) -> List<u16>§

    Source

    The UTF-16 code units of the string, no NUL (Windows W APIs take them; bare::ffi::WideCString adds the NUL).

  • pub const fn from_utf16<U: IntoIterator<Item = u16>>(units: U) throws (Utf16Error) -> str§

    Source

    UTF-16 code units (a List<u16>, Slice<u16> or [u16; N]) as a str; throws at the first unpaired surrogate.

  • pub const fn from_utf16_lossy<U: IntoIterator<Item = u16>>(units: U) -> str§

    Source

    from_utf16 with each unpaired surrogate replaced by U+FFFD.

  • pub const fn as_bytes(self) -> Slice<u8>§

    Source

    The UTF-8 bytes as a read-only view of the string itself (no copy, O(1)).

  • pub fn as_ptr(self) -> bare::ptr::ConstPtr<u8>§

    Source

    The first byte's address, for FFI (len() bytes, no NUL promised; an empty str gives a dangling pointer). Valid while a variable holding the string is in scope in a function where a pointer type appears; pin it to keep it longer.

  • pub const fn byte(self, i: usize) -> u8§

    Source

    Byte i, O(1); panics if i >= len().

  • pub const fn len(self) -> usize§

    Source

    Byte length (not the char count).

  • pub const fn is_empty(self) -> bool§

    Source

    true when the string has no bytes.

  • pub const fn char_count(self) -> usize§

    Source

    Number of chars, as s.chars().count().

  • pub const fn is_char_boundary(self, i: usize) -> bool§

    Source

    true when byte offset i starts a char or is len(), so s[i:] cannot panic.

  • pub const fn floor_char_boundary(self, i: usize) -> usize§

    Source

    The char boundary at or before byte i (len() past the end).

  • pub const fn ceil_char_boundary(self, i: usize) -> usize§

    Source

    The char boundary at or after byte i (len() past the end).

  • pub const fn get(self, r: Range<usize>) -> str | none§

    Source

    self[r.start:r.end] when both are char boundaries in order within len(), else none: the slice that never panics or clamps.

  • pub const fn char_slice(self, a: usize, b: usize) -> str§

    Source

    Chars a up to b (not included), counted from 0 like chars(); both clamp to char_count(), and a >= b gives "". O(b). By grapheme cluster: grapheme_slice.

  • pub const fn contains(self, pat: str) -> bool§

    Source

    true when pat occurs in self (always for an empty pat).

  • pub const fn starts_with(self, pat: str) -> bool§

    Source

    true when self begins with pat.

  • pub const fn ends_with(self, pat: str) -> bool§

    Source

    true when self ends with pat.

  • pub const fn find(self, pat: str) -> usize | none§

    Source

    Byte index of the first occurrence of pat; none if absent.

  • pub const fn rfind(self, pat: str) -> usize | none§

    Source

    Byte index of the last occurrence of pat; none if absent.

  • pub const fn strip_prefix(self, prefix: str) -> str | none§

    Source

    self without a leading prefix, or none if it does not start with it.

  • pub const fn strip_suffix(self, suffix: str) -> str | none§

    Source

    self without a trailing suffix, or none.

  • pub const fn split_once(self, sep: str) -> (str, str) | none§

    Source

    The pieces before and after the first sep, or none.

  • pub const fn repeat(self, n: usize) -> str§

    Source

    n copies back to back.

  • pub const fn trim(self) -> str§

    Source

    self without leading and trailing Unicode whitespace.

  • pub const fn trim_start(self) -> str§

    Source

    self without leading Unicode whitespace.

  • pub const fn trim_end(self) -> str§

    Source

    self without trailing Unicode whitespace.

  • pub const fn trim_matches(self, pat: str) -> str§

    Source

    self without every leading and trailing repeat of pat.

  • pub const fn trim_start_matches(self, pat: str) -> str§

    Source

    self without every leading repeat of pat (unchanged for an empty pat).

  • pub const fn trim_end_matches(self, pat: str) -> str§

    Source

    self without every trailing repeat of pat (unchanged for an empty pat).

  • pub const fn to_lowercase(self) -> str§

    Source

    Full Unicode case mapping ("ß" -> "SS", final σ -> ς); no locale rules.

  • pub const fn to_uppercase(self) -> str§

    Source

    Full Unicode uppercase mapping ("ß" -> "SS"); no locale rules.

  • pub const fn replace(self, from: str, to: str) -> str§

    Source

    A copy with every non-overlapping from replaced by to, left to right.

  • pub const fn chars(self) -> Chars§

    Source

    Unicode scalar values, in order.

  • pub const fn reversed(self) -> str§

    Source

    The characters in reverse order (code points; a combining mark moves with them). Python: s[::-1].

  • pub const fn count(self, pat: str) -> usize§

    Source

    The number of non-overlapping occurrences of pat, left to right; for an empty pat, one more than the number of characters (as Python's count).

  • pub const fn replacen(self, from: str, to: str, n: usize) -> str§

    Source

    replace of the first n occurrences only, left to right.

  • pub const fn to_titlecase(self) -> str§

    Source

    Each word's first letter in title case and its other letters in lowercase (full mappings). A word starts at a letter that does not follow a letter, a digit or an apostrophe. Python: s.title(), but "don't" stays one word.

  • pub fn bytes(self) -> Bytes§

    Source

    UTF-8 bytes, in order.

  • pub const fn split(self, sep: str) -> Split§

    Source

    The pieces between occurrences of sep (all of them, empty ones included).

  • pub const fn splitn(self, n: usize, sep: str) -> SplitN§

    Source

    At most n pieces split on sep from the start; the last one is the unsplit rest (n == 0 yields nothing).

  • pub const fn rsplit(self, sep: str) -> RSplit§

    Source

    The pieces split yields, last first.

  • pub const fn rsplitn(self, n: usize, sep: str) -> RSplitN§

    Source

    At most n pieces split on sep from the end, last first; the final one is the unsplit start.

  • pub const fn match_indices(self, pat: str) -> MatchIndices§

    Source

    (byte offset, match) for each non-overlapping pat, left to right; an empty pat matches nothing.

  • pub const fn lines(self) -> Lines§

    Source

    Lines without their \n / \r\n; a final line ending adds no empty line.

  • pub const fn char_indices(self) -> CharIndices§

    Source

    (byte offset, char) pairs.

  • pub const fn split_whitespace(self) -> SplitWhitespace§

    Source

    The non-empty pieces between runs of Unicode White_Space (the set trim strips).

  • pub const fn parse<T: FromStr>(self) throws (T::Err) -> T§

    Source

    T's value spelled by this string.

Trait implementations 11

impl char

  • pub const fn from_u32(n: u32) -> char | none§

    Source

    n as a char; none for a surrogate or a value past U+10FFFF. (as char takes only a u8.)

  • pub const unsafe fn from_u32_unchecked(n: u32) -> char§

    Source

    n as a char, unchecked: n must be a Unicode scalar value.

  • pub const fn from_digit(d: u32, radix: u32) -> char | none§

    Source

    Digit d in base radix (lowercase letters past 9); none if d is not a digit of that base. Panics unless radix is in 2..=36.

  • pub const fn to_digit(self, radix: u32) -> u32 | none§

    Source

    The value of this digit in base radix (either letter case); none if it is not one. Panics unless radix is in 2..=36.

  • pub const fn is_digit(self, radix: u32) -> bool§

    Source

    Panics unless radix is in 2..=36.

  • pub const fn len_utf8(self) -> usize§

    Source

    Bytes in UTF-8.

  • pub const fn len_utf16(self) -> usize§

    Source

    16-bit units in UTF-16.

  • pub const fn is_alphabetic(self) -> bool§

    Source

    Unicode Alphabetic.

  • pub const fn is_lowercase(self) -> bool§

    Source

    Unicode Lowercase.

  • pub const fn is_uppercase(self) -> bool§

    Source

    Unicode Uppercase.

  • pub const fn is_whitespace(self) -> bool§

    Source

    Unicode White_Space.

  • pub const fn is_numeric(self) -> bool§

    Source

    General category Nd, Nl or No.

  • pub const fn is_alphanumeric(self) -> bool§

    Source

    is_alphabetic or is_numeric.

  • pub const fn is_control(self) -> bool§

    Source

    General category Cc.

  • pub const fn to_lowercase(self) -> str§

    Source

    The full lowercase mapping, which may be several chars ('\u{130}' -> "i\u{307}"); locale-independent.

  • pub const fn to_uppercase(self) -> str§

    Source

    The full uppercase mapping, which may be several chars ('ß' -> "SS").

  • pub const fn to_titlecase(self) -> str§

    Source

    The full titlecase mapping, which may be several chars ('ß' -> "Ss", 'dž' -> "Dž"). Capitalize a word with c.to_titlecase() + rest.to_lowercase().

  • pub const fn to_simple_lowercase(self) -> char§

    Source

    The simple (one-char) lowercase mapping (UCD slc); self if none.

  • pub const fn to_simple_uppercase(self) -> char§

    Source

    The simple (one-char) uppercase mapping (UCD suc; 'ß' stays 'ß'); self if none.

  • pub const fn to_simple_titlecase(self) -> char§

    Source

    The simple (one-char) titlecase mapping (UCD stc; 'dž' -> 'Dž'); self if none.

  • pub const fn is_ascii(self) -> bool§

    Source

    true for U+0000 ..= U+007F.

  • pub const fn is_ascii_uppercase(self) -> bool§

    Source

    A ..= Z.

  • pub const fn is_ascii_lowercase(self) -> bool§

    Source

    a ..= z.

  • pub const fn is_ascii_alphabetic(self) -> bool§

    Source

    An ASCII letter.

  • pub const fn is_ascii_digit(self) -> bool§

    Source

    0 ..= 9.

  • pub const fn is_ascii_alphanumeric(self) -> bool§

    Source

    An ASCII letter or digit.

  • pub const fn is_ascii_hexdigit(self) -> bool§

    Source

    0 ..= 9, a ..= f or A ..= F.

  • pub const fn is_ascii_whitespace(self) -> bool§

    Source

    Space, \t, \n, form feed, \r.

  • pub const fn is_ascii_control(self) -> bool§

    Source

    U+0000 ..= U+001F or U+007F.

  • pub const fn is_ascii_graphic(self) -> bool§

    Source

    ! ..= ~ (printable, not space).

  • pub const fn is_ascii_punctuation(self) -> bool§

    Source

    An ASCII graphic char that is not a letter or digit.

  • pub const fn to_ascii_uppercase(self) -> char§

    Source

    The uppercase of an ASCII letter; any other char unchanged.

  • pub const fn to_ascii_lowercase(self) -> char§

    Source

    The lowercase of an ASCII letter; any other char unchanged.

  • pub const fn eq_ignore_ascii_case(self, other: char) -> bool§

    Source

    true when the two chars are equal up to ASCII case.

Trait implementations 4

impl Pinned

  • pub fn as_bytes_ptr(self) -> Ptr<u8>§

    Source

    The first byte (no NUL promised); dangling while there is no room.