mirror of
https://github.com/edg-l/edlang.git
synced 2024-11-09 09:38:24 +00:00
64 lines
22 KiB
HTML
64 lines
22 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="A trait for types that can be used to replace matches in a haystack."><title>Replacer in regex - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-dd39b87e5fcfba68.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="regex" data-themes="" data-resource-suffix="" data-rustdoc-version="1.80.0 (051478957 2024-07-21)" data-channel="1.80.0" data-search-js="search-d52510db62a78183.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../static.files/storage-118b08c4c78b968e.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-20a3ad099b048cf2.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-df360f571f6edeae.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc trait"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../regex/index.html">regex</a><span class="version">1.10.5</span></h2></div><h2 class="location"><a href="#">Replacer</a></h2><div class="sidebar-elems"><section><h3><a href="#required-methods">Required Methods</a></h3><ul class="block"><li><a href="#tymethod.replace_append">replace_append</a></li></ul><h3><a href="#provided-methods">Provided Methods</a></h3><ul class="block"><li><a href="#method.by_ref">by_ref</a></li><li><a href="#method.no_expansion">no_expansion</a></li></ul><h3><a href="#foreign-impls">Implementations on Foreign Types</a></h3><ul class="block"><li><a href="#impl-Replacer-for-%26Cow%3C'a,+str%3E">&'a Cow<'a, str></a></li><li><a href="#impl-Replacer-for-%26String">&'a String</a></li><li><a href="#impl-Replacer-for-%26str">&'a str</a></li><li><a href="#impl-Replacer-for-Cow%3C'a,+str%3E">Cow<'a, str></a></li><li><a href="#impl-Replacer-for-String">String</a></li></ul><h3><a href="#object-safety">Object Safety</a></h3><h3><a href="#implementors">Implementors</a></h3></section><h2><a href="index.html">In crate regex</a></h2></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Trait <a href="index.html">regex</a>::<wbr><a class="trait" href="#">Replacer</a><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><span class="out-of-band"><a class="src" href="../src/regex/regex/string.rs.html#2431-2477">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>−</span>]</button></span></div><pre class="rust item-decl"><code>pub trait Replacer {
|
||
// Required method
|
||
fn <a href="#tymethod.replace_append" class="fn">replace_append</a>(&mut self, caps: &<a class="struct" href="struct.Captures.html" title="struct regex::Captures">Captures</a><'_>, dst: &mut <a class="struct" href="https://doc.rust-lang.org/1.80.0/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>);
|
||
|
||
// Provided methods
|
||
fn <a href="#method.no_expansion" class="fn">no_expansion</a><'r>(&'r mut self) -> <a class="enum" href="https://doc.rust-lang.org/1.80.0/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="enum" href="https://doc.rust-lang.org/1.80.0/alloc/borrow/enum.Cow.html" title="enum alloc::borrow::Cow">Cow</a><'r, <a class="primitive" href="https://doc.rust-lang.org/1.80.0/std/primitive.str.html">str</a>>> { ... }
|
||
<span class="item-spacer"></span> fn <a href="#method.by_ref" class="fn">by_ref</a><'r>(&'r mut self) -> <a class="struct" href="struct.ReplacerRef.html" title="struct regex::ReplacerRef">ReplacerRef</a><'r, Self> { ... }
|
||
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A trait for types that can be used to replace matches in a haystack.</p>
|
||
<p>In general, users of this crate shouldn’t need to implement this trait,
|
||
since implementations are already provided for <code>&str</code> along with other
|
||
variants of string types, as well as <code>FnMut(&Captures) -> String</code> (or any
|
||
<code>FnMut(&Captures) -> T</code> where <code>T: AsRef<str></code>). Those cover most use cases,
|
||
but callers can implement this trait directly if necessary.</p>
|
||
<h2 id="example"><a class="doc-anchor" href="#example">§</a>Example</h2>
|
||
<p>This example shows a basic implementation of the <code>Replacer</code> trait. This
|
||
can be done much more simply using the replacement string interpolation
|
||
support (e.g., <code>$first $last</code>), but this approach avoids needing to parse
|
||
the replacement string at all.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>regex::{Captures, Regex, Replacer};
|
||
|
||
<span class="kw">struct </span>NameSwapper;
|
||
|
||
<span class="kw">impl </span>Replacer <span class="kw">for </span>NameSwapper {
|
||
<span class="kw">fn </span>replace_append(<span class="kw-2">&mut </span><span class="self">self</span>, caps: <span class="kw-2">&</span>Captures<<span class="lifetime">'_</span>>, dst: <span class="kw-2">&mut </span>String) {
|
||
dst.push_str(<span class="kw-2">&</span>caps[<span class="string">"first"</span>]);
|
||
dst.push_str(<span class="string">" "</span>);
|
||
dst.push_str(<span class="kw-2">&</span>caps[<span class="string">"last"</span>]);
|
||
}
|
||
}
|
||
|
||
<span class="kw">let </span>re = Regex::new(<span class="string">r"(?<last>[^,\s]+),\s+(?<first>\S+)"</span>).unwrap();
|
||
<span class="kw">let </span>result = re.replace(<span class="string">"Springsteen, Bruce"</span>, NameSwapper);
|
||
<span class="macro">assert_eq!</span>(result, <span class="string">"Bruce Springsteen"</span>);</code></pre></div>
|
||
</div></details><h2 id="required-methods" class="section-header">Required Methods<a href="#required-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="tymethod.replace_append" class="method"><a class="src rightside" href="../src/regex/regex/string.rs.html#2438">source</a><h4 class="code-header">fn <a href="#tymethod.replace_append" class="fn">replace_append</a>(&mut self, caps: &<a class="struct" href="struct.Captures.html" title="struct regex::Captures">Captures</a><'_>, dst: &mut <a class="struct" href="https://doc.rust-lang.org/1.80.0/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>)</h4></section></summary><div class="docblock"><p>Appends possibly empty data to <code>dst</code> to replace the current match.</p>
|
||
<p>The current match is represented by <code>caps</code>, which is guaranteed to
|
||
have a match at capture group <code>0</code>.</p>
|
||
<p>For example, a no-op replacement would be <code>dst.push_str(&caps[0])</code>.</p>
|
||
</div></details></div><h2 id="provided-methods" class="section-header">Provided Methods<a href="#provided-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="method.no_expansion" class="method"><a class="src rightside" href="../src/regex/regex/string.rs.html#2448-2450">source</a><h4 class="code-header">fn <a href="#method.no_expansion" class="fn">no_expansion</a><'r>(&'r mut self) -> <a class="enum" href="https://doc.rust-lang.org/1.80.0/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="enum" href="https://doc.rust-lang.org/1.80.0/alloc/borrow/enum.Cow.html" title="enum alloc::borrow::Cow">Cow</a><'r, <a class="primitive" href="https://doc.rust-lang.org/1.80.0/std/primitive.str.html">str</a>>></h4></section></summary><div class="docblock"><p>Return a fixed unchanging replacement string.</p>
|
||
<p>When doing replacements, if access to <a href="struct.Captures.html" title="struct regex::Captures"><code>Captures</code></a> is not needed (e.g.,
|
||
the replacement string does not need <code>$</code> expansion), then it can be
|
||
beneficial to avoid finding sub-captures.</p>
|
||
<p>In general, this is called once for every call to a replacement routine
|
||
such as <a href="struct.Regex.html#method.replace_all" title="method regex::Regex::replace_all"><code>Regex::replace_all</code></a>.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.by_ref" class="method"><a class="src rightside" href="../src/regex/regex/string.rs.html#2474-2476">source</a><h4 class="code-header">fn <a href="#method.by_ref" class="fn">by_ref</a><'r>(&'r mut self) -> <a class="struct" href="struct.ReplacerRef.html" title="struct regex::ReplacerRef">ReplacerRef</a><'r, Self></h4></section></summary><div class="docblock"><p>Returns a type that implements <code>Replacer</code>, but that borrows and wraps
|
||
this <code>Replacer</code>.</p>
|
||
<p>This is useful when you want to take a generic <code>Replacer</code> (which might
|
||
not be cloneable) and use it without consuming it, so it can be used
|
||
more than once.</p>
|
||
<h5 id="example-1"><a class="doc-anchor" href="#example-1">§</a>Example</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>regex::{Regex, Replacer};
|
||
|
||
<span class="kw">fn </span>replace_all_twice<R: Replacer>(
|
||
re: Regex,
|
||
src: <span class="kw-2">&</span>str,
|
||
<span class="kw-2">mut </span>rep: R,
|
||
) -> String {
|
||
<span class="kw">let </span>dst = re.replace_all(src, rep.by_ref());
|
||
<span class="kw">let </span>dst = re.replace_all(<span class="kw-2">&</span>dst, rep.by_ref());
|
||
dst.into_owned()
|
||
}</code></pre></div>
|
||
</div></details></div><h2 id="object-safety" class="section-header">Object Safety<a href="#object-safety" class="anchor">§</a></h2><div class="object-safety-info">This trait is <b>not</b> <a href="https://doc.rust-lang.org/1.80.0/reference/items/traits.html#object-safety">object safe</a>.</div><h2 id="foreign-impls" class="section-header">Implementations on Foreign Types<a href="#foreign-impls" class="anchor">§</a></h2><details class="toggle implementors-toggle"><summary><section id="impl-Replacer-for-String" class="impl"><a class="src rightside" href="../src/regex/regex/string.rs.html#2499-2507">source</a><a href="#impl-Replacer-for-String" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.Replacer.html" title="trait regex::Replacer">Replacer</a> for <a class="struct" href="https://doc.rust-lang.org/1.80.0/alloc/string/struct.String.html" title="struct alloc::string::String">String</a></h3></section></summary><div class="impl-items"><section id="method.replace_append" class="method trait-impl"><a class="src rightside" href="../src/regex/regex/string.rs.html#2500-2502">source</a><a href="#method.replace_append" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.replace_append" class="fn">replace_append</a>(&mut self, caps: &<a class="struct" href="struct.Captures.html" title="struct regex::Captures">Captures</a><'_>, dst: &mut <a class="struct" href="https://doc.rust-lang.org/1.80.0/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>)</h4></section><section id="method.no_expansion-1" class="method trait-impl"><a class="src rightside" href="../src/regex/regex/string.rs.html#2504-2506">source</a><a href="#method.no_expansion-1" class="anchor">§</a><h4 class="code-header">fn <a href="#method.no_expansion" class="fn">no_expansion</a>(&mut self) -> <a class="enum" href="https://doc.rust-lang.org/1.80.0/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="enum" href="https://doc.rust-lang.org/1.80.0/alloc/borrow/enum.Cow.html" title="enum alloc::borrow::Cow">Cow</a><'_, <a class="primitive" href="https://doc.rust-lang.org/1.80.0/std/primitive.str.html">str</a>>></h4></section></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Replacer-for-%26Cow%3C'a,+str%3E" class="impl"><a class="src rightside" href="../src/regex/regex/string.rs.html#2519-2527">source</a><a href="#impl-Replacer-for-%26Cow%3C'a,+str%3E" class="anchor">§</a><h3 class="code-header">impl<'a> <a class="trait" href="trait.Replacer.html" title="trait regex::Replacer">Replacer</a> for &'a <a class="enum" href="https://doc.rust-lang.org/1.80.0/alloc/borrow/enum.Cow.html" title="enum alloc::borrow::Cow">Cow</a><'a, <a class="primitive" href="https://doc.rust-lang.org/1.80.0/std/primitive.str.html">str</a>></h3></section></summary><div class="impl-items"><section id="method.replace_append-1" class="method trait-impl"><a class="src rightside" href="../src/regex/regex/string.rs.html#2520-2522">source</a><a href="#method.replace_append-1" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.replace_append" class="fn">replace_append</a>(&mut self, caps: &<a class="struct" href="struct.Captures.html" title="struct regex::Captures">Captures</a><'_>, dst: &mut <a class="struct" href="https://doc.rust-lang.org/1.80.0/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>)</h4></section><section id="method.no_expansion-2" class="method trait-impl"><a class="src rightside" href="../src/regex/regex/string.rs.html#2524-2526">source</a><a href="#method.no_expansion-2" class="anchor">§</a><h4 class="code-header">fn <a href="#method.no_expansion" class="fn">no_expansion</a>(&mut self) -> <a class="enum" href="https://doc.rust-lang.org/1.80.0/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="enum" href="https://doc.rust-lang.org/1.80.0/alloc/borrow/enum.Cow.html" title="enum alloc::borrow::Cow">Cow</a><'_, <a class="primitive" href="https://doc.rust-lang.org/1.80.0/std/primitive.str.html">str</a>>></h4></section></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Replacer-for-%26str" class="impl"><a class="src rightside" href="../src/regex/regex/string.rs.html#2479-2487">source</a><a href="#impl-Replacer-for-%26str" class="anchor">§</a><h3 class="code-header">impl<'a> <a class="trait" href="trait.Replacer.html" title="trait regex::Replacer">Replacer</a> for &'a <a class="primitive" href="https://doc.rust-lang.org/1.80.0/std/primitive.str.html">str</a></h3></section></summary><div class="impl-items"><section id="method.replace_append-2" class="method trait-impl"><a class="src rightside" href="../src/regex/regex/string.rs.html#2480-2482">source</a><a href="#method.replace_append-2" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.replace_append" class="fn">replace_append</a>(&mut self, caps: &<a class="struct" href="struct.Captures.html" title="struct regex::Captures">Captures</a><'_>, dst: &mut <a class="struct" href="https://doc.rust-lang.org/1.80.0/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>)</h4></section><section id="method.no_expansion-3" class="method trait-impl"><a class="src rightside" href="../src/regex/regex/string.rs.html#2484-2486">source</a><a href="#method.no_expansion-3" class="anchor">§</a><h4 class="code-header">fn <a href="#method.no_expansion" class="fn">no_expansion</a>(&mut self) -> <a class="enum" href="https://doc.rust-lang.org/1.80.0/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="enum" href="https://doc.rust-lang.org/1.80.0/alloc/borrow/enum.Cow.html" title="enum alloc::borrow::Cow">Cow</a><'_, <a class="primitive" href="https://doc.rust-lang.org/1.80.0/std/primitive.str.html">str</a>>></h4></section></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Replacer-for-%26String" class="impl"><a class="src rightside" href="../src/regex/regex/string.rs.html#2489-2497">source</a><a href="#impl-Replacer-for-%26String" class="anchor">§</a><h3 class="code-header">impl<'a> <a class="trait" href="trait.Replacer.html" title="trait regex::Replacer">Replacer</a> for &'a <a class="struct" href="https://doc.rust-lang.org/1.80.0/alloc/string/struct.String.html" title="struct alloc::string::String">String</a></h3></section></summary><div class="impl-items"><section id="method.replace_append-3" class="method trait-impl"><a class="src rightside" href="../src/regex/regex/string.rs.html#2490-2492">source</a><a href="#method.replace_append-3" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.replace_append" class="fn">replace_append</a>(&mut self, caps: &<a class="struct" href="struct.Captures.html" title="struct regex::Captures">Captures</a><'_>, dst: &mut <a class="struct" href="https://doc.rust-lang.org/1.80.0/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>)</h4></section><section id="method.no_expansion-4" class="method trait-impl"><a class="src rightside" href="../src/regex/regex/string.rs.html#2494-2496">source</a><a href="#method.no_expansion-4" class="anchor">§</a><h4 class="code-header">fn <a href="#method.no_expansion" class="fn">no_expansion</a>(&mut self) -> <a class="enum" href="https://doc.rust-lang.org/1.80.0/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="enum" href="https://doc.rust-lang.org/1.80.0/alloc/borrow/enum.Cow.html" title="enum alloc::borrow::Cow">Cow</a><'_, <a class="primitive" href="https://doc.rust-lang.org/1.80.0/std/primitive.str.html">str</a>>></h4></section></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Replacer-for-Cow%3C'a,+str%3E" class="impl"><a class="src rightside" href="../src/regex/regex/string.rs.html#2509-2517">source</a><a href="#impl-Replacer-for-Cow%3C'a,+str%3E" class="anchor">§</a><h3 class="code-header">impl<'a> <a class="trait" href="trait.Replacer.html" title="trait regex::Replacer">Replacer</a> for <a class="enum" href="https://doc.rust-lang.org/1.80.0/alloc/borrow/enum.Cow.html" title="enum alloc::borrow::Cow">Cow</a><'a, <a class="primitive" href="https://doc.rust-lang.org/1.80.0/std/primitive.str.html">str</a>></h3></section></summary><div class="impl-items"><section id="method.replace_append-4" class="method trait-impl"><a class="src rightside" href="../src/regex/regex/string.rs.html#2510-2512">source</a><a href="#method.replace_append-4" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.replace_append" class="fn">replace_append</a>(&mut self, caps: &<a class="struct" href="struct.Captures.html" title="struct regex::Captures">Captures</a><'_>, dst: &mut <a class="struct" href="https://doc.rust-lang.org/1.80.0/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>)</h4></section><section id="method.no_expansion-5" class="method trait-impl"><a class="src rightside" href="../src/regex/regex/string.rs.html#2514-2516">source</a><a href="#method.no_expansion-5" class="anchor">§</a><h4 class="code-header">fn <a href="#method.no_expansion" class="fn">no_expansion</a>(&mut self) -> <a class="enum" href="https://doc.rust-lang.org/1.80.0/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="enum" href="https://doc.rust-lang.org/1.80.0/alloc/borrow/enum.Cow.html" title="enum alloc::borrow::Cow">Cow</a><'_, <a class="primitive" href="https://doc.rust-lang.org/1.80.0/std/primitive.str.html">str</a>>></h4></section></div></details><h2 id="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"><section id="impl-Replacer-for-ReplacerRef%3C'a,+R%3E" class="impl"><a class="src rightside" href="../src/regex/regex/string.rs.html#2548-2556">source</a><a href="#impl-Replacer-for-ReplacerRef%3C'a,+R%3E" class="anchor">§</a><h3 class="code-header">impl<'a, R: <a class="trait" href="trait.Replacer.html" title="trait regex::Replacer">Replacer</a> + ?<a class="trait" href="https://doc.rust-lang.org/1.80.0/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + 'a> <a class="trait" href="trait.Replacer.html" title="trait regex::Replacer">Replacer</a> for <a class="struct" href="struct.ReplacerRef.html" title="struct regex::ReplacerRef">ReplacerRef</a><'a, R></h3></section><section id="impl-Replacer-for-NoExpand%3C's%3E" class="impl"><a class="src rightside" href="../src/regex/regex/string.rs.html#2580-2588">source</a><a href="#impl-Replacer-for-NoExpand%3C's%3E" class="anchor">§</a><h3 class="code-header">impl<'s> <a class="trait" href="trait.Replacer.html" title="trait regex::Replacer">Replacer</a> for <a class="struct" href="struct.NoExpand.html" title="struct regex::NoExpand">NoExpand</a><'s></h3></section><section id="impl-Replacer-for-F" class="impl"><a class="src rightside" href="../src/regex/regex/string.rs.html#2529-2537">source</a><a href="#impl-Replacer-for-F" class="anchor">§</a><h3 class="code-header">impl<F, T> <a class="trait" href="trait.Replacer.html" title="trait regex::Replacer">Replacer</a> for F<div class="where">where
|
||
F: <a class="trait" href="https://doc.rust-lang.org/1.80.0/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(&<a class="struct" href="struct.Captures.html" title="struct regex::Captures">Captures</a><'_>) -> T,
|
||
T: <a class="trait" href="https://doc.rust-lang.org/1.80.0/core/convert/trait.AsRef.html" title="trait core::convert::AsRef">AsRef</a><<a class="primitive" href="https://doc.rust-lang.org/1.80.0/std/primitive.str.html">str</a>>,</div></h3></section></div><script src="../trait.impl/regex/regex/string/trait.Replacer.js" data-ignore-extern-crates="alloc,std" async></script></section></div></main></body></html> |