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§
Sourcefn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String)
fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String)
Append the replacement for the match caps to dst.
Provided Methods§
Sourcefn no_expansion(&mut self) -> Option<Cow<'_, str>>
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.
Sourcefn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
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".