Skip to main content

Replacer

Trait Replacer 

Source
pub trait Replacer {
    // Required method
    fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String);

    // Provided methods
    fn no_expansion(&mut self) -> Option<Cow<'_, str>> { ... }
    fn by_ref(&mut self) -> &mut Self
       where Self: Sized { ... }
}
Expand description

Produces the text that replaces one match in Regex::replace and its relatives.

Implemented for &str, String, &String and Cow<str>, which expand $ references with Captures::expand; for NoExpand, which inserts its text unchanged; and for closures that map a Captures to a string.

§Examples

use ferroni::prelude::*;

let re = Regex::new(r"(?<word>\w+)@").unwrap();
assert_eq!(re.replace_all("ada@ grace@", "${word}"), "ada grace");
assert_eq!(re.replace_all("ada@", |caps: &Captures| caps["word"].to_uppercase()), "ADA");
assert_eq!(re.replace_all("ada@", NoExpand("$word")), "$word");

Required Methods§

Source

fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String)

Append the replacement for the match caps to dst.

Provided Methods§

Source

fn no_expansion(&mut self) -> Option<Cow<'_, str>>

The replacement as a fixed string, when it needs no expansion.

Returning Some lets the replace methods skip capture groups and use the cheaper find_iter. Return None (the default) when the replacement depends on the match.

Source

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

A mutable reference to this replacer, for passing it by reference.

use ferroni::prelude::*;

let re = Regex::new(r"a").unwrap();
let mut rep = |_: &Captures| String::from("b");
assert_eq!(re.replace_all("aa", rep.by_ref()), "bb");
assert_eq!(re.replace_all("aa", rep), "bb");

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Replacer for &String

Source§

fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String)

Source§

fn no_expansion(&mut self) -> Option<Cow<'_, str>>

Source§

impl Replacer for &str

Source§

fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String)

Source§

fn no_expansion(&mut self) -> Option<Cow<'_, str>>

Source§

impl Replacer for Cow<'_, str>

Source§

fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String)

Source§

fn no_expansion(&mut self) -> Option<Cow<'_, str>>

Source§

impl Replacer for String

Source§

fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String)

Source§

fn no_expansion(&mut self) -> Option<Cow<'_, str>>

Implementors§

Source§

impl Replacer for NoExpand<'_>

Source§

impl<F, T> Replacer for F
where F: FnMut(&Captures<'_>) -> T, T: AsRef<str>,