String

Struct String 

Source
pub struct String(/* private fields */);

Implementations§

Source§

impl String

Source

pub const fn new() -> Self

Creates a new empty String.

Given that the String is empty, this will not allocate any initial buffer. While that means that this initial operation is very inexpensive, it may cause excessive allocation later when you add data.

§Examples

Basic usage:

let s = eztd_core::String::new();
Source

pub fn byte_len(&self) -> usize

Returns the length of this String, in bytes, not chars or graphemes. In other words, it may not be what a human considers the length of the string.

§Examples

Basic usage:

let a = eztd_core::String::from("foo");
assert_eq!(a.byte_len(), 3);
Source

pub fn is_empty(&self) -> bool

Returns true if self has a length of zero bytes.

§Examples

Basic usage:

let s = eztd_core::String::from("");
assert!(s.is_empty());

let s = eztd_core::String::from("not empty");
assert!(!s.is_empty());
Source

pub fn char_len(&self) -> usize

Returns the length of this String, in bytes, not chars or graphemes. In other words, it may not be what a human considers the length of the string.

§Examples

Basic usage:

let a = eztd_core::String::from("foo");
assert_eq!(a.char_len(), 3);
Source

pub fn len(&self) -> usize

👎Deprecated: Use either byte_len or char_len to be more explicit on meaning
Source

pub fn get(&self, range: impl RangeBounds<usize>) -> Option<Self>

Returns a subslice of String.

This is the non-panicking alternative to indexing the String. Returns None whenever equivalent indexing operation would panic.

§Examples
let v = eztd_core::String::from("Hello World");

assert_eq!(Some(eztd_core::String::from("Hell")), v.get(0..4));
Source

pub fn split_at(&self, mid: usize) -> (Self, Self)

Divide one string slice into two at an index.

The argument, mid, should be a byte offset from the start of the string. It must also be on the boundary of a UTF-8 code point.

The two slices returned go from the start of the string slice to mid, and from mid to the end of the string slice.

To get mutable string slices instead, see the split_at_mut method.

§Panics

Panics if mid is not on a UTF-8 code point boundary, or if it is past the end of the last code point of the string slice.

§Examples

Basic usage:

let s = eztd_core::String::from("Per Martin");

let (first, last) = s.split_at(3);

assert_eq!("Per", first);
assert_eq!(" Martin", last);
Source

pub fn bytes(&self) -> Bytes

An iterator over the bytes of a string slice.

As a string slice consists of a sequence of bytes, we can iterate through a string slice by byte. This method returns such an iterator.

§Examples

Basic usage:

let mut bytes = eztd_core::String::from("bors").bytes();

assert_eq!(Some(b'b'), bytes.next());
assert_eq!(Some(b'o'), bytes.next());
assert_eq!(Some(b'r'), bytes.next());
assert_eq!(Some(b's'), bytes.next());

assert_eq!(None, bytes.next());
Source

pub fn trim_start(&self) -> Self

Returns a string slice with leading whitespace removed.

‘Whitespace’ is defined according to the terms of the Unicode Derived Core Property White_Space.

§Text directionality

A string is a sequence of bytes. start in this context means the first position of that byte string; for a left-to-right language like English or Russian, this will be left side, and for right-to-left languages like Arabic or Hebrew, this will be the right side.

§Examples

Basic usage:

let s = " Hello\tworld\t";
assert_eq!("Hello\tworld\t", s.trim_start());
Source

pub fn join_str(&self, string: impl AsRef<str>) -> Self

Appends a given string onto the end of this String.

§Examples

Basic usage:

let s = eztd_core::String::from("foo");

let s = s.join_str("bar");
assert_eq!("foobar", s);

let baz = eztd_core::String::from("baz");
let s = s.join_str(baz);

assert_eq!("foobarbaz", s);
Source

pub fn join_char(&self, ch: char) -> Self

Appends the given char to the end of this String.

§Examples

Basic usage:

let s = eztd_core::String::from("abc");

let s = s.join_char('1').join_char('2').join_char('3');

assert_eq!("abc123", s);
Source

pub fn shrink_to_fit(&self) -> String

Shrinks the capacity of this String to match its length.

§Examples

Basic usage:

let s = eztd_core::String::from("foo");

let s = s.shrink_to_fit();
Source§

impl String

Transitional Python API

Source

pub fn lstrip(&self) -> Self

👎Deprecated: In Rust, we refer to this as trim_start
Source§

impl String

Interop

Source

pub fn as_str(&self) -> &str

Extracts a string slice containing the entire String.

§Examples

Basic usage:

let s = eztd_core::String::from("foo");

assert_eq!("foo", s.as_str());

Trait Implementations§

Source§

impl<'s, S: AsRef<str>> Add<S> for &'s String

Implements the + operator for concatenating two strings.

This consumes the String on the left-hand side and re-uses its buffer (growing it if necessary). This is done to avoid allocating a new String and copying the entire contents on every operation, which would lead to O(n^2) running time when building an n-byte string by repeated concatenation.

The string on the right-hand side is only borrowed; its contents are copied into the returned String.

§Examples

Concatenating two Strings takes the first by value and borrows the second:

let a = eztd_core::String::from("hello");
let b = eztd_core::String::from(" world");
let c = &a + &b + "foo";
Source§

type Output = String

The resulting type after applying the + operator.
Source§

fn add(self, other: S) -> String

Performs the + operation. Read more
Source§

impl<S: AsRef<str>> Add<S> for String

Source§

type Output = String

The resulting type after applying the + operator.
Source§

fn add(self, other: S) -> String

Performs the + operation. Read more
Source§

impl AsRef<str> for String

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for String

Source§

fn clone(&self) -> String

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for String

Source§

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

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

impl Default for String

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for String

Source§

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

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

impl<'s> From<&'s String> for String

Source§

fn from(other: &'s String) -> Self

Converts to this type from the input type.
Source§

impl<'s> From<&'s str> for String

Source§

fn from(other: &'s str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for String

Source§

fn from(other: String) -> Self

Converts to this type from the input type.
Source§

impl From<char> for String

Source§

fn from(other: char) -> Self

Converts to this type from the input type.
Source§

impl<'a> FromIterator<&'a char> for String

Source§

fn from_iter<I: IntoIterator<Item = &'a char>>(iter: I) -> String

Creates a value from an iterator. Read more
Source§

impl<'a> FromIterator<&'a str> for String

Source§

fn from_iter<I: IntoIterator<Item = &'a str>>(iter: I) -> String

Creates a value from an iterator. Read more
Source§

impl FromIterator<String> for String

Source§

fn from_iter<I: IntoIterator<Item = String>>(iter: I) -> String

Creates a value from an iterator. Read more
Source§

impl FromIterator<char> for String

Source§

fn from_iter<I: IntoIterator<Item = char>>(iter: I) -> String

Creates a value from an iterator. Read more
Source§

impl FromStr for String

Source§

type Err = Infallible

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<String, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for String

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Index<Range<usize>> for String

Source§

type Output = str

The returned type after indexing.
Source§

fn index(&self, index: Range<usize>) -> &str

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<RangeFrom<usize>> for String

Source§

type Output = str

The returned type after indexing.
Source§

fn index(&self, index: RangeFrom<usize>) -> &str

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<RangeFull> for String

Source§

type Output = str

The returned type after indexing.
Source§

fn index(&self, index: RangeFull) -> &str

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<RangeInclusive<usize>> for String

Source§

type Output = str

The returned type after indexing.
Source§

fn index(&self, index: RangeInclusive<usize>) -> &str

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<RangeTo<usize>> for String

Source§

type Output = str

The returned type after indexing.
Source§

fn index(&self, index: RangeTo<usize>) -> &str

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<RangeToInclusive<usize>> for String

Source§

type Output = str

The returned type after indexing.
Source§

fn index(&self, index: RangeToInclusive<usize>) -> &str

Performs the indexing (container[index]) operation. Read more
Source§

impl Ord for String

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<'a, 'b> PartialEq<&'a String> for String

Source§

fn eq(&self, other: &&'a String) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, 'b> PartialEq<&'a str> for String

Source§

fn eq(&self, other: &&'a str) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, 'b> PartialEq<String> for &'a String

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, 'b> PartialEq<String> for &'a str

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, 'b> PartialEq<String> for String

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, 'b> PartialEq<String> for String

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, 'b> PartialEq<String> for str

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, 'b> PartialEq<str> for String

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for String

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for String

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for String

Auto Trait Implementations§

§

impl Freeze for String

§

impl RefUnwindSafe for String

§

impl Send for String

§

impl Sync for String

§

impl Unpin for String

§

impl UnwindSafe for String

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 = Infallible

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.