Trait Replacer
pub trait Replacer
A trait for types that can be used to replace matches in a haystack.
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, as well as FnMut(&Captures) -> String (or any
FnMut(&Captures) -> T where T: AsRef<str>). Those cover most use cases,
but callers can implement this trait directly if necessary.
Example
This example shows a basic implementation of the Replacer trait. This
can be done much more simply using the replacement string interpolation
support (e.g., $first $last), but this approach avoids needing to parse
the replacement string at all.
use ;
;
let re = new.unwrap;
let result = re.replace;
assert_eq!;
Required Methods
fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String)Appends possibly empty data to
dstto replace the current match.The current match is represented by
caps, which is guaranteed to have a match at capture group0.For example, a no-op replacement would be
dst.push_str(&caps[0]).
Provided Methods
fn no_expansion<'r>(&'r mut self) -> Option<Cow<'r, str>>Return a fixed unchanging replacement string.
When doing replacements, if access to
Capturesis not needed (e.g., the replacement 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 a replacement routine such as
Regex::replace_all.fn by_ref<'r>(&'r mut self) -> ReplacerRef<'r, Self>Returns a type that implements
Replacer, but that borrows and wraps thisReplacer.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 ;
Implementors
impl Replacer for Stringimpl<'a> Replacer for &'a Cow<'a, str>impl<'a> Replacer for &'a Stringimpl<'a> Replacer for &'a strimpl<'a> Replacer for Cow<'a, str>impl<'a, R: Replacer + ?Sized + 'a> Replacer for ReplacerRef<'a, R>impl<'s> Replacer for NoExpand<'s>impl<F, T> Replacer for F where F: FnMut(&Captures<'_>) -> T, T: AsRef<str>,