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) -> ReplacerRef<'_, Self> { ... }
}
Expand description

Replacer describes types that can be used to replace matches in a string.

In general, users of this crate shouldn’t need to implement this trait, since implementations are already provided for &str along with other variants of string types and FnMut(&Captures) -> String (or any FnMut(&Captures) -> T where T: AsRef<str>), which covers most use cases.

Required Methods§

Source

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

Appends text to dst to replace the current match.

The current match is represented by caps, which is guaranteed to have a match at capture group 0.

For example, a no-op replacement would be dst.push_str(caps.get(0).unwrap().as_str()).

Provided Methods§

Source

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

Return a fixed unchanging replacement string.

When doing replacements, if access to Captures is not needed (e.g., the replacement byte string does not need $ expansion), then it can be beneficial to avoid finding sub-captures.

In general, this is called once for every call to replacen.

Source

fn by_ref(&mut self) -> ReplacerRef<'_, Self>

Return a Replacer that borrows and wraps this Replacer.

This is useful when you want to take a generic Replacer (which might not be cloneable) and use it without consuming it, so it can be used more than once.

§Example
use fancy_regex::{Regex, Replacer};

fn replace_all_twice<R: Replacer>(
    re: Regex,
    src: &str,
    mut rep: R,
) -> String {
    let dst = re.replace_all(src, rep.by_ref());
    let dst = re.replace_all(&dst, rep.by_ref());
    dst.into_owned()
}

Dyn Compatibility§

This trait is not 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 String

Source§

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

Source§

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

Source§

impl<'a> Replacer for &'a Cow<'a, str>

Source§

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

Source§

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

Source§

impl<'a> Replacer for Cow<'a, str>

Source§

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

Source§

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

Implementors§

Source§

impl<'a, R: Replacer + ?Sized + 'a> Replacer for ReplacerRef<'a, R>

Source§

impl<'t> Replacer for NoExpand<'t>

Source§

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