core::str
本頁內容
- Modules
- Structs
Utf8Error2Utf16Error2- Traits
FromStr1- Type aliases
- Implementations
impl str50as_bytesas_ptrbytebytesceil_char_boundarychar_countchar_indiceschar_slicecharscontainscountencode_utf16ends_withfindfloor_char_boundaryfrom_utf16from_utf16_lossyfrom_utf8from_utf8_lossygetis_char_boundaryis_emptylenlinesmatch_indicesnewparserepeatreplacereplacenreversedrfindrsplitrsplitnsplitsplit_oncesplit_whitespacesplitnstarts_withstrip_prefixstrip_suffixto_lowercaseto_titlecaseto_uppercasetrimtrim_endtrim_end_matchestrim_matchestrim_starttrim_start_matches
impl char34eq_ignore_ascii_casefrom_digitfrom_u32from_u32_uncheckedis_alphabeticis_alphanumericis_asciiis_ascii_alphabeticis_ascii_alphanumericis_ascii_controlis_ascii_digitis_ascii_graphicis_ascii_hexdigitis_ascii_lowercaseis_ascii_punctuationis_ascii_uppercaseis_ascii_whitespaceis_controlis_digitis_lowercaseis_numericis_uppercaseis_whitespacelen_utf16len_utf8to_ascii_lowercaseto_ascii_uppercaseto_digitto_lowercaseto_simple_lowercaseto_simple_titlecaseto_simple_uppercaseto_titlecaseto_uppercase
impl Pinned1
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
unicodeUnicode text onstr: normalization forms, full case folding, and segmentation into grapheme clusters (user-perceived characters) and words.
Structs
CharsSource
pub struct CharsWhat s.chars() returns: the Unicode scalar values, in order.
SplitSource
pub struct SplitWhat s.split(sep) returns: the pieces between occurrences of sep,
left to right.
SplitNSource
pub struct SplitNWhat s.splitn(n, sep) returns: at most n pieces, the last one the
unsplit rest.
RSplitSource
pub struct RSplitWhat s.rsplit(sep) returns: the pieces of split, last first.
RSplitNSource
pub struct RSplitNWhat s.rsplitn(n, sep) returns: at most n pieces from the end, the
last one the unsplit start.
MatchIndicesSource
pub struct MatchIndicesWhat s.match_indices(pat) returns: (byte offset, match) pairs, left
to right.
LinesSource
pub struct LinesWhat s.lines() returns: the lines without their \n / \r\n.
CharIndicesSource
pub struct CharIndicesWhat s.char_indices() returns: (byte offset, char) pairs, in order.
SplitWhitespaceSource
pub struct SplitWhitespaceWhat s.split_whitespace() returns: the non-empty pieces between runs
of whitespace, left to right.
StrBufSource
pub struct StrBufA growable UTF-8 buffer. A handle like the collections: copies share the buffer, one object holding the length, the capacity and the bytes.
An empty buffer with room for 16 bytes.
An empty buffer with room for
capbytes.Byte length.
truewhen the buffer holds no bytes.Bytes the buffer holds before it grows.
Empty the buffer; its capacity stays.
Room for
additionalmore bytes (doubling).Append
c, UTF-8 encoded.Append
s, growing the buffer as needed.Append one byte.
unsafe:to_strhands out the bytes as astr, which must be UTF-8; the caller keeps them so by the time it is called. Bytes of unknown encoding go throughstr::from_utf8.The current contents as an immutable
str(a copy).
Utf8ErrorSource
pub struct Utf8ErrorBytes that are not UTF-8 (str::from_utf8, RawStr::to_str,
RawPath::to_path; Windows paths: not UTF-16).
Length of the valid prefix, in bytes (a Windows path's: UTF-16 units).
What went wrong, for display.
Utf16ErrorSource
pub struct Utf16ErrorUTF-16 with an unpaired surrogate (str::from_utf16,
bare::ffi::WideCStr::to_str).
Length of the valid prefix, in units.
What went wrong, for display.
Traits
FromStrSource
pub trait FromStrstr.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
The empty string.
bytesas astr; throws if they are not UTF-8.bytesas astrwith 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).The UTF-16 code units of the string, no NUL (Windows
WAPIs take them;bare::ffi::WideCStringadds the NUL).
Sourcepub const fn from_utf16<U: IntoIterator<Item = u16>>(§units: U) throws (Utf16Error) -> str UTF-16 code units (a
List<u16>,Slice<u16>or[u16; N]) as astr; throws at the first unpaired surrogate.
Sourcepub const fn from_utf16_lossy<U: IntoIterator<Item = u16>>(§units: U) -> str from_utf16with each unpaired surrogate replaced by U+FFFD.The UTF-8 bytes as a read-only view of the string itself (no copy, O(1)).
The first byte's address, for FFI (
len()bytes, no NUL promised; an emptystrgives 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.Byte
i, O(1); panics ifi >= len().Byte length (not the char count).
truewhen the string has no bytes.Number of chars, as
s.chars().count().truewhen byte offsetistarts a char or islen(), sos[i:]cannot panic.The char boundary at or before byte
i(len()past the end).The char boundary at or after byte
i(len()past the end).self[r.start:r.end]when both are char boundaries in order withinlen(), elsenone: the slice that never panics or clamps.Chars
aup tob(not included), counted from 0 likechars(); both clamp tochar_count(), anda >= bgives"". O(b). By grapheme cluster:grapheme_slice.truewhenpatoccurs inself(always for an emptypat).truewhenselfbegins withpat.truewhenselfends withpat.Byte index of the first occurrence of
pat;noneif absent.Byte index of the last occurrence of
pat;noneif absent.selfwithout a leadingprefix, ornoneif it does not start with it.selfwithout a trailingsuffix, ornone.The pieces before and after the first
sep, ornone.ncopies back to back.selfwithout leading and trailing Unicode whitespace.selfwithout leading Unicode whitespace.selfwithout trailing Unicode whitespace.selfwithout every leading and trailing repeat ofpat.selfwithout every leading repeat ofpat(unchanged for an emptypat).selfwithout every trailing repeat ofpat(unchanged for an emptypat).Full Unicode case mapping (
"ß"->"SS", finalσ->ς); no locale rules.Full Unicode uppercase mapping (
"ß"->"SS"); no locale rules.A copy with every non-overlapping
fromreplaced byto, left to right.Unicode scalar values, in order.
The characters in reverse order (code points; a combining mark moves with them). Python:
s[::-1].The number of non-overlapping occurrences of
pat, left to right; for an emptypat, one more than the number of characters (as Python'scount).replaceof the firstnoccurrences only, left to right.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.UTF-8 bytes, in order.
The pieces between occurrences of
sep(all of them, empty ones included).At most
npieces split onsepfrom the start; the last one is the unsplit rest (n == 0yields nothing).The pieces
splityields, last first.At most
npieces split onsepfrom the end, last first; the final one is the unsplit start.
Sourcepub const fn match_indices(§self, pat: str) -> MatchIndices (byte offset, match)for each non-overlappingpat, left to right; an emptypatmatches nothing.Lines without their
\n/\r\n; a final line ending adds no empty line.
Sourcepub const fn char_indices(§self) -> CharIndices (byte offset, char)pairs.
Sourcepub const fn split_whitespace(§self) -> SplitWhitespace The non-empty pieces between runs of Unicode
White_Space(the settrimstrips).T's value spelled by this string.
Trait implementations 11
impl Sliceable<Range<usize>> for strimpl Add for strimpl Add<char> for strimpl PartialEq for strimpl PartialOrd for strimpl Eq for strimpl Ord for strimpl Hash for strimpl FromIterator<char> for strimpl FromIterator<str> for strimpl FromStr for str
impl char
nas a char;nonefor a surrogate or a value past U+10FFFF. (as chartakes only au8.)nas a char, unchecked:nmust be a Unicode scalar value.Digit
din baseradix(lowercase letters past9);noneifdis not a digit of that base. Panics unlessradixis in 2..=36.The value of this digit in base
radix(either letter case);noneif it is not one. Panics unlessradixis in 2..=36.Panics unless
radixis in 2..=36.Bytes in UTF-8.
16-bit units in UTF-16.
Unicode
Alphabetic.Unicode
Lowercase.Unicode
Uppercase.Unicode
White_Space.General category
Nd,NlorNo.is_alphabeticoris_numeric.General category
Cc.The full lowercase mapping, which may be several chars (
'\u{130}'->"i\u{307}"); locale-independent.The full uppercase mapping, which may be several chars (
'ß'->"SS").The full titlecase mapping, which may be several chars (
'ß'->"Ss",'dž'->"Dž"). Capitalize a word withc.to_titlecase() + rest.to_lowercase().The simple (one-char) lowercase mapping (UCD
slc);selfif none.The simple (one-char) uppercase mapping (UCD
suc;'ß'stays'ß');selfif none.The simple (one-char) titlecase mapping (UCD
stc;'dž'->'Dž');selfif none.truefor U+0000 ..= U+007F.A..=Z.a..=z.An ASCII letter.
0..=9.An ASCII letter or digit.
0..=9,a..=forA..=F.Space,
\t,\n, form feed,\r.U+0000 ..= U+001F or U+007F.
!..=~(printable, not space).An ASCII graphic char that is not a letter or digit.
The uppercase of an ASCII letter; any other char unchanged.
The lowercase of an ASCII letter; any other char unchanged.
truewhen the two chars are equal up to ASCII case.
Trait implementations 4
impl PartialOrd for charimpl Ord for charimpl TryFrom<u32> for charimpl FromStr for char
impl Pinned
The first byte (no NUL promised); dangling while there is no room.