Skip to main content

replace_chars

Function replace_chars 

Source
pub fn replace_chars<F>(s: &str, replacer: F) -> Cow<'_, str>
where F: Fn(char) -> Option<char>,
Expand description

Replace characters efficiently with a mapping function

This function applies character replacements without unnecessary allocations when no replacements are needed. Uses a single-pass algorithm that borrows when no changes are needed and only allocates on first replacement found.

§Arguments

  • s - The input string
  • replacer - Function that maps characters to their replacements

§Returns

A string with replacements applied, borrowing when no changes are needed