pub trait IndexedStr:
Display
+ Debug
+ PartialEq<IndexedString>
+ for<'a> PartialEq<IndexedSlice<'a>> {
Show 15 methods
// Required methods
fn as_str(&self) -> &str;
fn as_slice(&self) -> IndexedSlice<'_>;
fn len(&self) -> usize;
fn byte_len(&self) -> usize;
fn char_at(&self, index: usize) -> Option<char>;
fn slice<R: RangeBounds<usize>>(&self, range: R) -> IndexedSlice<'_>;
fn chars(&self) -> &[char];
fn to_indexed_string(&self) -> IndexedString;
fn lines(&self) -> IndexedLines<'_> ⓘ;
// Provided methods
fn is_empty(&self) -> bool { ... }
fn to_lowercase(&self) -> IndexedString { ... }
fn to_uppercase(&self) -> IndexedString { ... }
fn starts_with<S: AsRef<str>>(&self, s: S) -> bool { ... }
fn ends_with<S: AsRef<str>>(&self, s: S) -> bool { ... }
fn parse<F>(&self) -> Result<F, <F as FromStr>::Err>
where F: FromStr { ... }
}
Expand description
A trait that facilitates safe interaction with strings that contain multi-byte characters.
IndexedString
replaces String
, whereas IndexedSlice
replaces &str
.
Both of these types as well as anything that implements IndexedStr
are characterized by
the fact that their len()
and indexing methods operate on characters, not bytes, and
enough information is stored to allow for O(1) slicing and indexing on a character and
byte basis as needed, but the default interface is character-centric.
This all comes at the cost of increased memory usage and some performance overhead when a
IndexedString
is created, but the overhead is minimal when using IndexedSlice
or
any other type implementing IndexedStr
.
It is also worth noting that all of these types will never panic when indexing or slicing,
unlike &str
and String
, and clamped bounds are used instead.
Required Methods§
Sourcefn as_str(&self) -> &str
fn as_str(&self) -> &str
Returns a &str
representation of this IndexedStr
.
WARNING: Once you cast to a &str
, you are leaving the safety provided by
IndexedStr
. Only use this method when you need to interface with code that requires
a &str
.
Sourcefn as_slice(&self) -> IndexedSlice<'_>
fn as_slice(&self) -> IndexedSlice<'_>
Returns a IndexedSlice
that represents the entire contents of this IndexedStr
.
Sourcefn len(&self) -> usize
fn len(&self) -> usize
Returns the length of this IndexedStr
in characters, NOT bytes.
Sourcefn byte_len(&self) -> usize
fn byte_len(&self) -> usize
Returns the byte length of this IndexedStr
. This will be longer than the
len
if the string contains multi-byte characters.
Sourcefn char_at(&self, index: usize) -> Option<char>
fn char_at(&self, index: usize) -> Option<char>
Returns the character at the given index, if it exists.
Sourcefn slice<R: RangeBounds<usize>>(&self, range: R) -> IndexedSlice<'_>
fn slice<R: RangeBounds<usize>>(&self, range: R) -> IndexedSlice<'_>
Returns a sub-slice of this IndexedStr
based on the given range in terms of the
characters in the string, not bytes.
The range is automatically clamped to the bounds of the IndexedStr
.
Sourcefn chars(&self) -> &[char]
fn chars(&self) -> &[char]
Returns a slice containing all characters of this IndexedStr
in order.
Sourcefn to_indexed_string(&self) -> IndexedString
fn to_indexed_string(&self) -> IndexedString
Converts this IndexedStr
into an owned, dynamically allocated IndexedString
.
Sourcefn lines(&self) -> IndexedLines<'_> ⓘ
fn lines(&self) -> IndexedLines<'_> ⓘ
Returns an iterator over the lines of this IndexedStr
.
Provided Methods§
Sourcefn is_empty(&self) -> bool
fn is_empty(&self) -> bool
Returns true
if this IndexedStr
is empty (of length 0).
Sourcefn to_lowercase(&self) -> IndexedString
fn to_lowercase(&self) -> IndexedString
Returns a new IndexedStr
that is the lowercase version of this IndexedStr
.
Sourcefn to_uppercase(&self) -> IndexedString
fn to_uppercase(&self) -> IndexedString
Returns a new IndexedStr
that is the uppercase version of this IndexedStr
.
Sourcefn starts_with<S: AsRef<str>>(&self, s: S) -> bool
fn starts_with<S: AsRef<str>>(&self, s: S) -> bool
Returns true
if this IndexedStr
starts with the given string.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.