Trait mut_str::StrExt

source ·
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§

source

type Output: ?Sized

The output string type for string operations.

source

type Iter<'a>: Iterator<Item = &'a Char> where Self: 'a

The iterator type for iterating over Char references.

source

type IterMut<'a>: Iterator<Item = &'a mut Char> where Self: 'a

The iterator type for iterating over mutable Char references.

Required Methods§

source

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');
source

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');
source

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");
source

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");
source

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!");
source

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!");
source

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));
source

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));
source

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);
source

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 and r, when utf8 encoded, do not have the same length, LenNotEqual will be returned.
source

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 than s, when utf8 encoded, ReplacementTooLong will be returned.
source

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
source

fn replace_with_pad_char<'a, C>( &'a mut self, r: &str, pad_char: C ) -> Result<&'a mut str, ReplaceWithPadCharError>
where C: Into<char>,

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
source

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 than s, when utf8 encoded, ReplacementTooLong will be returned.
source

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
source

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>,

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

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl StrExt for str

§

type Output = str

§

type Iter<'a> = CharRefs<'a>

§

type IterMut<'a> = CharMutRefs<'a>

source§

fn get_char(&self, i: usize) -> Option<&Char>

source§

fn get_char_mut(&mut self, i: usize) -> Option<&mut Char>

source§

fn char_slice<R: RangeBounds<usize>>(&self, range: R) -> Option<&Self::Output>

source§

fn char_slice_mut<R: RangeBounds<usize>>( &mut self, range: R ) -> Option<&mut Self::Output>

source§

fn char_split_at(&self, mid: usize) -> Option<(&Self::Output, &Self::Output)>

source§

fn char_split_at_mut( &mut self, mid: usize ) -> Option<(&mut Self::Output, &mut Self::Output)>

source§

fn ref_iter(&self) -> Self::Iter<'_>

source§

fn mut_iter(&mut self) -> Self::IterMut<'_>

source§

fn copy_to<'a>(&self, buffer: &'a mut [u8]) -> Option<&'a mut Self::Output>

source§

fn replace_with<'a>(&'a mut self, r: &str) -> Result<&'a mut str, LenNotEqual>

source§

fn replace_with_pad_space<'a>( &'a mut self, r: &str ) -> Result<&'a mut str, ReplacementTooLong>

source§

fn replace_with_pad<'a>( &'a mut self, r: &str, pad: u8 ) -> Result<&'a mut str, ReplaceWithPadError>

source§

fn replace_with_pad_char<'a, C>( &'a mut self, r: &str, pad_char: C ) -> Result<&'a mut str, ReplaceWithPadCharError>
where C: Into<char>,

source§

fn replace_with_pad_left_space( &mut self, r: &str ) -> Result<&mut Self::Output, ReplacementTooLong>

source§

fn replace_with_pad_left( &mut self, r: &str, pad: u8 ) -> Result<&mut Self::Output, ReplaceWithPadError>

source§

fn replace_with_pad_left_char<C>( &mut self, r: &str, pad_char: C ) -> Result<&mut Self::Output, ReplaceWithPadCharError>
where C: Into<char>,

Implementors§