ReplaceStringInPlace

Trait ReplaceStringInPlace 

Source
pub trait ReplaceStringInPlace {
    // Required methods
    fn remove_all_ascii_in_place(&mut self, ch: AsciiChar);
    fn replace_all_ascii_in_place(&mut self, from: AsciiChar, to: AsciiChar);
}
Expand description

Trait for in-place string replacement operations.

This trait provides methods that modify the string directly without creating new allocations when possible. These operations are more memory-efficient but modify the original string.

Required Methods§

Source

fn remove_all_ascii_in_place(&mut self, ch: AsciiChar)

Removes all occurrences of the specified ASCII character from the string in-place.

This method modifies the string directly, potentially reducing its length. For Cow<str>, this may convert a borrowed string to an owned string if modifications are needed.

§Arguments
  • ch - The ASCII character to remove from the string
§Examples
use cow_replace::ReplaceStringInPlace;
use ascii::AsciiChar;

let mut text = "hello world".to_string();
text.remove_all_ascii_in_place(AsciiChar::l);
assert_eq!(text, "heo word");

// Works with empty results too
let mut text = "lllll".to_string();
text.remove_all_ascii_in_place(AsciiChar::l);
assert_eq!(text, "");
Source

fn replace_all_ascii_in_place(&mut self, from: AsciiChar, to: AsciiChar)

Replaces all occurrences of one ASCII character with another in-place.

This method modifies the string directly by replacing bytes. Since both characters are ASCII, the string length remains the same.

§Arguments
  • from - The ASCII character to search for and replace
  • to - The ASCII character to replace with
§Examples
use cow_replace::ReplaceStringInPlace;
use ascii::AsciiChar;

let mut text = "hello world".to_string();
text.replace_all_ascii_in_place(AsciiChar::l, AsciiChar::x);
assert_eq!(text, "hexxo worxd");

// No change if character not found
let mut text = "hello world".to_string();
text.replace_all_ascii_in_place(AsciiChar::z, AsciiChar::x);
assert_eq!(text, "hello world");

Implementations on Foreign Types§

Source§

impl ReplaceStringInPlace for Cow<'_, str>

Source§

impl ReplaceStringInPlace for String

Implementors§