pub trait StrExt {
type Output: ?Sized;
type Iter<'a>: Iterator<Item = &'a Char>
where Self: 'a;
type IterMut<'a>: Iterator<Item = &'a mut Char>
where Self: 'a;
Show 16 methods
// Required methods
fn get_char(&self, i: usize) -> Option<&Char>;
fn get_char_mut(&mut self, i: usize) -> Option<&mut Char>;
fn char_slice<R: RangeBounds<usize>>(
&self,
range: R
) -> Option<&Self::Output>;
fn char_slice_mut<R: RangeBounds<usize>>(
&mut self,
range: R
) -> Option<&mut Self::Output>;
fn char_split_at(
&self,
mid: usize
) -> Option<(&Self::Output, &Self::Output)>;
fn char_split_at_mut(
&mut self,
mid: usize
) -> Option<(&mut Self::Output, &mut Self::Output)>;
fn ref_iter(&self) -> Self::Iter<'_>;
fn mut_iter(&mut self) -> Self::IterMut<'_>;
fn copy_to<'a>(&self, buffer: &'a mut [u8]) -> Option<&'a mut Self::Output>;
fn replace_with<'a>(
&'a mut self,
r: &str
) -> Result<&'a mut str, LenNotEqual>;
fn replace_with_pad_space<'a>(
&'a mut self,
r: &str
) -> Result<&'a mut str, ReplacementTooLong>;
fn replace_with_pad<'a>(
&'a mut self,
r: &str,
pad: u8
) -> Result<&'a mut str, ReplaceWithPadError>;
fn replace_with_pad_char<'a, C>(
&'a mut self,
r: &str,
pad_char: C
) -> Result<&'a mut str, ReplaceWithPadCharError>
where C: Into<char>;
fn replace_with_pad_left_space<'a>(
&'a mut self,
r: &str
) -> Result<&'a mut Self::Output, ReplacementTooLong>;
fn replace_with_pad_left<'a>(
&'a mut self,
r: &str,
pad: u8
) -> Result<&'a mut Self::Output, ReplaceWithPadError>;
fn replace_with_pad_left_char<'a, C>(
&'a mut self,
r: &str,
pad_char: C
) -> Result<&'a mut Self::Output, ReplaceWithPadCharError>
where C: Into<char>;
}
Expand description
The StrExt
trait adds some methods to string types, many of which operate on character indexes and with character references.
To use its methods, import it with a use statement.
use mut_str::StrExt;
Required Associated Types§
Required Methods§
sourcefn get_char(&self, i: usize) -> Option<&Char>
fn get_char(&self, i: usize) -> Option<&Char>
Get a character reference from the string and an index.
use mut_str::StrExt;
let s = "Hello, World!";
let c = s.get_char(1).unwrap();
assert_eq!(c, 'e');
sourcefn get_char_mut(&mut self, i: usize) -> Option<&mut Char>
fn get_char_mut(&mut self, i: usize) -> Option<&mut Char>
Get a mutable character reference from the mutable string and an index.
use mut_str::StrExt;
let mut owned_s = Box::<str>::from("Hello, World!");
let c = owned_s.get_char_mut(1).unwrap();
assert_eq!(c, 'e');
sourcefn char_slice<R: RangeBounds<usize>>(&self, range: R) -> Option<&Self::Output>
fn char_slice<R: RangeBounds<usize>>(&self, range: R) -> Option<&Self::Output>
Slice the string in units of UTF-8 characters.
If range
is out of bounds, None
will be returned.
use mut_str::StrExt;
let s = "Hello, World!";
let hello = s.char_slice(..5).unwrap();
assert_eq!(hello, "Hello");
let world = s.char_slice(7..12).unwrap();
assert_eq!(world, "World");
sourcefn char_slice_mut<R: RangeBounds<usize>>(
&mut self,
range: R
) -> Option<&mut Self::Output>
fn char_slice_mut<R: RangeBounds<usize>>( &mut self, range: R ) -> Option<&mut Self::Output>
Slice the mutable string in units of UTF-8 characters.
If range
is out of bounds, None
will be returned.
use mut_str::StrExt;
let mut owned_s = Box::<str>::from("Hello, World!");
let hello = owned_s.char_slice_mut(..5).unwrap();
assert_eq!(hello, "Hello");
let world = owned_s.char_slice_mut(7..12).unwrap();
assert_eq!(world, "World");
sourcefn char_split_at(&self, mid: usize) -> Option<(&Self::Output, &Self::Output)>
fn char_split_at(&self, mid: usize) -> Option<(&Self::Output, &Self::Output)>
Divide the string into two at an index in units of UTF-8 characters.
If mid
is out of bounds, None
will be returned.
use mut_str::StrExt;
let s = "Hello, World!";
let (l, r) = s.char_split_at(6).unwrap();
assert_eq!(l, "Hello,");
assert_eq!(r, " World!");
sourcefn char_split_at_mut(
&mut self,
mid: usize
) -> Option<(&mut Self::Output, &mut Self::Output)>
fn char_split_at_mut( &mut self, mid: usize ) -> Option<(&mut Self::Output, &mut Self::Output)>
Divide the mutable string into two at an index in units of UTF-8 characters.
If mid
is out of bounds, None
will be returned.
use mut_str::StrExt;
let mut owned_s = Box::<str>::from("Hello, World!");
let (l, r) = owned_s.char_split_at_mut(6).unwrap();
assert_eq!(l, "Hello,");
assert_eq!(r, " World!");
sourcefn ref_iter(&self) -> Self::Iter<'_>
fn ref_iter(&self) -> Self::Iter<'_>
Get an iterator over character references in the string.
use mut_str::StrExt;
let s = "Hello, World!";
s.ref_iter()
.zip(s.chars())
.for_each(|(x, y)| assert_eq!(x, y));
sourcefn mut_iter(&mut self) -> Self::IterMut<'_>
fn mut_iter(&mut self) -> Self::IterMut<'_>
Get an iterator over mutable character references in the string.
use mut_str::StrExt;
let s = "Hello, World!";
let mut owned_s = Box::<str>::from(s);
owned_s.mut_iter()
.zip(s.chars())
.for_each(|(x, y)| assert_eq!(x, y));
sourcefn copy_to<'a>(&self, buffer: &'a mut [u8]) -> Option<&'a mut Self::Output>
fn copy_to<'a>(&self, buffer: &'a mut [u8]) -> Option<&'a mut Self::Output>
Copy the string to a byte buffer and get the new string containing the inserted character.
Returns None
if buffer
is shorter than the string.
use mut_str::StrExt;
let s = "Hello, World!";
let mut buffer = [0; 50];
let new_s = s.copy_to(&mut buffer).unwrap();
assert_eq!(new_s, s);
sourcefn replace_with<'a>(&'a mut self, r: &str) -> Result<&'a mut str, LenNotEqual>
fn replace_with<'a>(&'a mut self, r: &str) -> Result<&'a mut str, LenNotEqual>
Replace the mutable string with another of the same length.
use mut_str::StrExt;
let mut owned_s = Box::<str>::from("World!");
owned_s.replace_with("🌍!!").unwrap();
assert_eq!(&*owned_s, "🌍!!");
owned_s.replace_with("aaaaaa").unwrap();
assert_eq!(&*owned_s, "aaaaaa");
§Errors
- If
s
andr
, when utf8 encoded, do not have the same length,LenNotEqual
will be returned.
sourcefn replace_with_pad_space<'a>(
&'a mut self,
r: &str
) -> Result<&'a mut str, ReplacementTooLong>
fn replace_with_pad_space<'a>( &'a mut self, r: &str ) -> Result<&'a mut str, ReplacementTooLong>
Replace the mutable string with another of the same length or shorter. The remaining bytes will be filled with spaces.
use mut_str::StrExt;
let mut owned_s = Box::<str>::from("World!");
owned_s.replace_with_pad_space("🌍").unwrap();
assert_eq!(&*owned_s, "🌍 ");
owned_s.replace_with_pad_space("aaaa").unwrap();
assert_eq!(&*owned_s, "aaaa ");
§Errors
- If
r
, when utf8 encoded, is longer thans
, when utf8 encoded,ReplacementTooLong
will be returned.
sourcefn replace_with_pad<'a>(
&'a mut self,
r: &str,
pad: u8
) -> Result<&'a mut str, ReplaceWithPadError>
fn replace_with_pad<'a>( &'a mut self, r: &str, pad: u8 ) -> Result<&'a mut str, ReplaceWithPadError>
Replace the mutable string with another of the same length or shorter.
The remaining bytes will be filled with pad
.
use mut_str::StrExt;
let mut owned_s = Box::<str>::from("World!");
owned_s.replace_with_pad("🌍", b'!').unwrap();
assert_eq!(&*owned_s, "🌍!!");
owned_s.replace_with_pad("aaaa", b'b').unwrap();
assert_eq!(&*owned_s, "aaaabb");
§Errors
- If
pad
is not valid utf8,ReplaceWithPadError::InvalidPad
will be returned. - If
r
, when utf8 encoded, is longer thans
, when utf8 encoded,ReplaceWithPadError::ReplacementLen
will be returned.
sourcefn replace_with_pad_char<'a, C>(
&'a mut self,
r: &str,
pad_char: C
) -> Result<&'a mut str, ReplaceWithPadCharError>
fn replace_with_pad_char<'a, C>( &'a mut self, r: &str, pad_char: C ) -> Result<&'a mut str, ReplaceWithPadCharError>
Replace the mutable string with another of the same length or shorter.
The remaining bytes will be filled with pad
, which must be one byte long.
use mut_str::StrExt;
let mut owned_s = Box::<str>::from("World!");
owned_s.replace_with_pad_char("🌍", '!').unwrap();
assert_eq!(&*owned_s, "🌍!!");
owned_s.replace_with_pad_char("aaaa", 'b').unwrap();
assert_eq!(&*owned_s, "aaaabb");
§Errors
- If
pad_char
, when utf8 encoded, is longer thanSelf
,ReplaceWithPadCharError::PadCharTooLong
will be returned. - If
r
, when utf8 encoded, is longer thans
, when utf8 encoded,ReplaceWithPadCharError::ReplacementLen
will be returned.
sourcefn replace_with_pad_left_space<'a>(
&'a mut self,
r: &str
) -> Result<&'a mut Self::Output, ReplacementTooLong>
fn replace_with_pad_left_space<'a>( &'a mut self, r: &str ) -> Result<&'a mut Self::Output, ReplacementTooLong>
Replace the mutable string with another of the same length or shorter, right aligned. The remaining bytes before the string will be filled with spaces.
use mut_str::StrExt;
let mut owned_s = Box::<str>::from("World!");
owned_s.replace_with_pad_left_space("🌍").unwrap();
assert_eq!(&*owned_s, " 🌍");
owned_s.replace_with_pad_left_space("aaaa").unwrap();
assert_eq!(&*owned_s, " aaaa");
§Errors
- If
r
, when utf8 encoded, is longer thans
, when utf8 encoded,ReplacementTooLong
will be returned.
sourcefn replace_with_pad_left<'a>(
&'a mut self,
r: &str,
pad: u8
) -> Result<&'a mut Self::Output, ReplaceWithPadError>
fn replace_with_pad_left<'a>( &'a mut self, r: &str, pad: u8 ) -> Result<&'a mut Self::Output, ReplaceWithPadError>
Replace the mutable string with another of the same length or shorter, right aligned.
The remaining bytes before the string will be filled with pad
.
use mut_str::StrExt;
let mut owned_s = Box::<str>::from("World!");
owned_s.replace_with_pad_left("🌍", b'!').unwrap();
assert_eq!(&*owned_s, "!!🌍");
owned_s.replace_with_pad_left("aaaa", b'b').unwrap();
assert_eq!(&*owned_s, "bbaaaa");
§Errors
- If
pad
is not valid utf8,ReplaceWithPadError::InvalidPad
will be returned. - If
r
, when utf8 encoded, is longer thans
, when utf8 encoded,ReplaceWithPadError::ReplacementLen
will be returned.
sourcefn replace_with_pad_left_char<'a, C>(
&'a mut self,
r: &str,
pad_char: C
) -> Result<&'a mut Self::Output, ReplaceWithPadCharError>
fn replace_with_pad_left_char<'a, C>( &'a mut self, r: &str, pad_char: C ) -> Result<&'a mut Self::Output, ReplaceWithPadCharError>
Replace the mutable string with another of the same length or shorter, right aligned.
The remaining bytes before the string will be filled with char
, which must be one byte long.
use mut_str::StrExt;
let mut owned_s = Box::<str>::from("World!");
owned_s.replace_with_pad_left_char("🌍", '!').unwrap();
assert_eq!(&*owned_s, "!!🌍");
owned_s.replace_with_pad_left_char("aaaa", 'b').unwrap();
assert_eq!(&*owned_s, "bbaaaa");
§Errors
- If
pad_char
, when utf8 encoded, is longer thanSelf
,ReplaceWithPadCharError::PadCharTooLong
will be returned. - If
r
, when utf8 encoded, is longer thans
, when utf8 encoded,ReplaceWithPadCharError::ReplacementLen
will be returned.