Skip to main content

Str

Enum Str 

Source
pub enum Str {
    Utf8(Box<str>),
    Wide(Box<[u32]>),
}
Expand description

A Python string, which is a sequence of code points.

Nearly every string in nearly every program is valid UTF-8 and takes the first arm, which is a Box<str> and costs nothing. '\ud800' is a lone surrogate, which is a perfectly ordinary Python string and something a Rust str cannot hold, so a string containing one takes the second arm and spends four bytes a code point. Paying that for every string to serve the few that need it would be the wrong trade, and refusing them, which is what this did until now, is worse: 58 files in CPython’s own standard library have one.

The two arms are never both valid for the same string. A Wide is built only after a surrogate has arrived, so Utf8 and Wide never hold the same sequence and PartialEq can stay derived.

Variants§

§

Utf8(Box<str>)

The usual case.

§

Wide(Box<[u32]>)

Code points, for a string holding at least one lone surrogate.

Implementations§

Source§

impl Str

Source

pub fn code_points(&self) -> impl Iterator<Item = u32> + '_

The code points, in order, whatever the string is stored as.

Source

pub fn repr(&self) -> String

What CPython’s repr prints for this string.

Source

pub fn len(&self) -> usize

How many code points, which is what len answers for a str.

Linear for a Utf8 string, since UTF-8 does not carry a count. CPython stores one in the object header and answers in constant time, and the representation that will do the same here is the one in the spec rather than this one.

Source

pub fn code_point_at(&self, index: usize) -> Option<u32>

The code point at an offset, or None past the end.

Linear in the offset for a string that is not ASCII, because UTF-8 has no way to reach the nth code point except by counting to it. Anything walking a whole string wants Str::code_points instead, which counts once.

Source

pub fn is_empty(&self) -> bool

Whether this is the empty string, which decides Str vs JoinedStr.

Trait Implementations§

Source§

impl Clone for Str

Source§

fn clone(&self) -> Str

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Str

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Display for Str

Source§

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

The text itself, which is what str gives back and what print writes.

A lone surrogate has no UTF-8 encoding, and CPython raises UnicodeEncodeError rather than writing one. Until there is an encoder to raise it from, one is written as the replacement character, which is what every other tool that has to keep going does.

Source§

impl Eq for Str

Source§

impl From<&str> for Str

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Str

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Str

Source§

fn eq(&self, other: &Str) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Str

Auto Trait Implementations§

§

impl Freeze for Str

§

impl RefUnwindSafe for Str

§

impl Send for Str

§

impl Sync for Str

§

impl Unpin for Str

§

impl UnsafeUnpin for Str

§

impl UnwindSafe for Str

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.