pub struct String(/* private fields */);Implementations§
Source§impl String
impl String
Sourcepub const fn new() -> Self
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();Sourcepub fn is_empty(&self) -> bool
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());pub fn len(&self) -> usize
byte_len or char_len to be more explicit on meaningSourcepub fn get(&self, range: impl RangeBounds<usize>) -> Option<Self>
pub fn get(&self, range: impl RangeBounds<usize>) -> Option<Self>
Sourcepub fn split_at(&self, mid: usize) -> (Self, Self)
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);Sourcepub fn bytes(&self) -> Bytes ⓘ
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());Sourcepub fn trim_start(&self) -> Self
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());Sourcepub fn join_str(&self, string: impl AsRef<str>) -> Self
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);Sourcepub fn shrink_to_fit(&self) -> String
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();Trait Implementations§
Source§impl<'s, S: AsRef<str>> Add<S> for &'s String
Implements the + operator for concatenating two strings.
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";