blob: 9168a24f159747a152ddae1cb430ba9439df2f48 [file] [log] [blame]
<!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="Replacer describes types that can be used to replace matches in a string."><meta name="keywords" content="rust, rustlang, rust-lang, Replacer"><title>Replacer in regex - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../normalize.css"><link rel="stylesheet" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../ayu.css" disabled><link rel="stylesheet" href="../dark.css" disabled><link rel="stylesheet" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.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">&#9776;</button><a class="sidebar-logo" href="../regex/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../regex/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><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-%26%27a%20Cow%3C%27a%2C%20str%3E">&amp;&#39;a Cow&lt;&#39;a, str&gt;</a></li><li><a href="#impl-Replacer-for-%26%27a%20String">&amp;&#39;a String</a></li><li><a href="#impl-Replacer-for-%26%27a%20str">&amp;&#39;a str</a></li><li><a href="#impl-Replacer-for-Cow%3C%27a%2C%20str%3E">Cow&lt;&#39;a, str&gt;</a></li><li><a href="#impl-Replacer-for-String">String</a></li></ul><h3><a href="#implementors">Implementors</a></h3></section><h2><a href="index.html">In regex</a></h2></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Trait <a href="index.html">regex</a>::<wbr><a class="trait" href="#">Replacer</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../src/regex/re_unicode.rs.html#1266-1311">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><div class="item-decl"><pre class="rust trait"><code>pub trait Replacer {
fn <a href="#tymethod.replace_append" class="fnname">replace_append</a>(&amp;mut self, caps: &amp;<a class="struct" href="struct.Captures.html" title="struct regex::Captures">Captures</a>&lt;'_&gt;, dst: &amp;mut String);
fn <a href="#method.no_expansion" class="fnname">no_expansion</a>&lt;'r&gt;(&amp;'r mut self) -&gt; Option&lt;Cow&lt;'r, str&gt;&gt; { ... }
<span class="item-spacer"></span> fn <a href="#method.by_ref" class="fnname">by_ref</a>&lt;'r&gt;(&amp;'r mut self) -&gt; <a class="struct" href="struct.ReplacerRef.html" title="struct regex::ReplacerRef">ReplacerRef</a>&lt;'r, Self&gt; { ... }
}</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Replacer describes types that can be used to replace matches in a string.</p>
<p>In general, users of this crate shouldn’t need to implement this trait,
since implementations are already provided for <code>&amp;str</code> along with other
variants of string types and <code>FnMut(&amp;Captures) -&gt; String</code> (or any
<code>FnMut(&amp;Captures) -&gt; T</code> where <code>T: AsRef&lt;str&gt;</code>), which covers most use cases.</p>
</div></details><h2 id="required-methods" class="small-section-header">Required Methods<a href="#required-methods" class="anchor"></a></h2><div class="methods"><details class="rustdoc-toggle method-toggle" open><summary><section id="tymethod.replace_append" class="method has-srclink"><a class="srclink rightside" href="../src/regex/re_unicode.rs.html#1274">source</a><h4 class="code-header">fn <a href="#tymethod.replace_append" class="fnname">replace_append</a>(&amp;mut self, caps: &amp;<a class="struct" href="struct.Captures.html" title="struct regex::Captures">Captures</a>&lt;'_&gt;, dst: &amp;mut String)</h4></section></summary><div class="docblock"><p>Appends text 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.get(0).unwrap().as_str())</code>.</p>
</div></details></div><h2 id="provided-methods" class="small-section-header">Provided Methods<a href="#provided-methods" class="anchor"></a></h2><div class="methods"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.no_expansion" class="method has-srclink"><a class="srclink rightside" href="../src/regex/re_unicode.rs.html#1283-1285">source</a><h4 class="code-header">fn <a href="#method.no_expansion" class="fnname">no_expansion</a>&lt;'r&gt;(&amp;'r mut self) -&gt; Option&lt;Cow&lt;'r, str&gt;&gt;</h4></section></summary><div class="docblock"><p>Return a fixed unchanging replacement string.</p>
<p>When doing replacements, if access to <code>Captures</code> is not needed (e.g.,
the replacement byte 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 <code>replacen</code>.</p>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.by_ref" class="method has-srclink"><a class="srclink rightside" href="../src/regex/re_unicode.rs.html#1308-1310">source</a><h4 class="code-header">fn <a href="#method.by_ref" class="fnname">by_ref</a>&lt;'r&gt;(&amp;'r mut self) -&gt; <a class="struct" href="struct.ReplacerRef.html" title="struct regex::ReplacerRef">ReplacerRef</a>&lt;'r, Self&gt;</h4></section></summary><div class="docblock"><p>Return a <code>Replacer</code> 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"><a href="#example">Example</a></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&lt;R: Replacer&gt;(
re: Regex,
src: <span class="kw-2">&amp;</span>str,
<span class="kw-2">mut </span>rep: R,
) -&gt; 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">&amp;</span>dst, rep.by_ref());
dst.into_owned()
}</code></pre></div>
</div></details></div><h2 id="foreign-impls" class="small-section-header">Implementations on Foreign Types<a href="#foreign-impls" class="anchor"></a></h2><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Replacer-for-%26%27a%20str" class="impl has-srclink"><a class="srclink rightside" href="../src/regex/re_unicode.rs.html#1328-1336">source</a><a href="#impl-Replacer-for-%26%27a%20str" class="anchor"></a><h3 class="code-header">impl&lt;'a&gt; <a class="trait" href="trait.Replacer.html" title="trait regex::Replacer">Replacer</a> for &amp;'a str</h3></section></summary><div class="impl-items"><section id="method.replace_append" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/regex/re_unicode.rs.html#1329-1331">source</a><a href="#method.replace_append" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.replace_append" class="fnname">replace_append</a>(&amp;mut self, caps: &amp;<a class="struct" href="struct.Captures.html" title="struct regex::Captures">Captures</a>&lt;'_&gt;, dst: &amp;mut String)</h4></section><section id="method.no_expansion-1" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/regex/re_unicode.rs.html#1333-1335">source</a><a href="#method.no_expansion-1" class="anchor"></a><h4 class="code-header">fn <a href="#method.no_expansion" class="fnname">no_expansion</a>(&amp;mut self) -&gt; Option&lt;Cow&lt;'_, str&gt;&gt;</h4></section></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Replacer-for-%26%27a%20String" class="impl has-srclink"><a class="srclink rightside" href="../src/regex/re_unicode.rs.html#1338-1346">source</a><a href="#impl-Replacer-for-%26%27a%20String" class="anchor"></a><h3 class="code-header">impl&lt;'a&gt; <a class="trait" href="trait.Replacer.html" title="trait regex::Replacer">Replacer</a> for &amp;'a String</h3></section></summary><div class="impl-items"><section id="method.replace_append-1" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/regex/re_unicode.rs.html#1339-1341">source</a><a href="#method.replace_append-1" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.replace_append" class="fnname">replace_append</a>(&amp;mut self, caps: &amp;<a class="struct" href="struct.Captures.html" title="struct regex::Captures">Captures</a>&lt;'_&gt;, dst: &amp;mut String)</h4></section><section id="method.no_expansion-2" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/regex/re_unicode.rs.html#1343-1345">source</a><a href="#method.no_expansion-2" class="anchor"></a><h4 class="code-header">fn <a href="#method.no_expansion" class="fnname">no_expansion</a>(&amp;mut self) -&gt; Option&lt;Cow&lt;'_, str&gt;&gt;</h4></section></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Replacer-for-String" class="impl has-srclink"><a class="srclink rightside" href="../src/regex/re_unicode.rs.html#1348-1356">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 String</h3></section></summary><div class="impl-items"><section id="method.replace_append-2" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/regex/re_unicode.rs.html#1349-1351">source</a><a href="#method.replace_append-2" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.replace_append" class="fnname">replace_append</a>(&amp;mut self, caps: &amp;<a class="struct" href="struct.Captures.html" title="struct regex::Captures">Captures</a>&lt;'_&gt;, dst: &amp;mut String)</h4></section><section id="method.no_expansion-3" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/regex/re_unicode.rs.html#1353-1355">source</a><a href="#method.no_expansion-3" class="anchor"></a><h4 class="code-header">fn <a href="#method.no_expansion" class="fnname">no_expansion</a>(&amp;mut self) -&gt; Option&lt;Cow&lt;'_, str&gt;&gt;</h4></section></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Replacer-for-Cow%3C%27a%2C%20str%3E" class="impl has-srclink"><a class="srclink rightside" href="../src/regex/re_unicode.rs.html#1358-1366">source</a><a href="#impl-Replacer-for-Cow%3C%27a%2C%20str%3E" class="anchor"></a><h3 class="code-header">impl&lt;'a&gt; <a class="trait" href="trait.Replacer.html" title="trait regex::Replacer">Replacer</a> for Cow&lt;'a, str&gt;</h3></section></summary><div class="impl-items"><section id="method.replace_append-3" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/regex/re_unicode.rs.html#1359-1361">source</a><a href="#method.replace_append-3" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.replace_append" class="fnname">replace_append</a>(&amp;mut self, caps: &amp;<a class="struct" href="struct.Captures.html" title="struct regex::Captures">Captures</a>&lt;'_&gt;, dst: &amp;mut String)</h4></section><section id="method.no_expansion-4" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/regex/re_unicode.rs.html#1363-1365">source</a><a href="#method.no_expansion-4" class="anchor"></a><h4 class="code-header">fn <a href="#method.no_expansion" class="fnname">no_expansion</a>(&amp;mut self) -&gt; Option&lt;Cow&lt;'_, str&gt;&gt;</h4></section></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Replacer-for-%26%27a%20Cow%3C%27a%2C%20str%3E" class="impl has-srclink"><a class="srclink rightside" href="../src/regex/re_unicode.rs.html#1368-1376">source</a><a href="#impl-Replacer-for-%26%27a%20Cow%3C%27a%2C%20str%3E" class="anchor"></a><h3 class="code-header">impl&lt;'a&gt; <a class="trait" href="trait.Replacer.html" title="trait regex::Replacer">Replacer</a> for &amp;'a Cow&lt;'a, str&gt;</h3></section></summary><div class="impl-items"><section id="method.replace_append-4" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/regex/re_unicode.rs.html#1369-1371">source</a><a href="#method.replace_append-4" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.replace_append" class="fnname">replace_append</a>(&amp;mut self, caps: &amp;<a class="struct" href="struct.Captures.html" title="struct regex::Captures">Captures</a>&lt;'_&gt;, dst: &amp;mut String)</h4></section><section id="method.no_expansion-5" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/regex/re_unicode.rs.html#1373-1375">source</a><a href="#method.no_expansion-5" class="anchor"></a><h4 class="code-header">fn <a href="#method.no_expansion" class="fnname">no_expansion</a>(&amp;mut self) -&gt; Option&lt;Cow&lt;'_, str&gt;&gt;</h4></section></div></details><h2 id="implementors" class="small-section-header">Implementors<a href="#implementors" class="anchor"></a></h2><div id="implementors-list"><section id="impl-Replacer-for-ReplacerRef%3C%27a%2C%20R%3E" class="impl has-srclink"><a class="srclink rightside" href="../src/regex/re_unicode.rs.html#1319-1326">source</a><a href="#impl-Replacer-for-ReplacerRef%3C%27a%2C%20R%3E" class="anchor"></a><h3 class="code-header">impl&lt;'a, R:&nbsp;<a class="trait" href="trait.Replacer.html" title="trait regex::Replacer">Replacer</a> + ?Sized + 'a&gt; <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>&lt;'a, R&gt;</h3></section><section id="impl-Replacer-for-NoExpand%3C%27t%3E" class="impl has-srclink"><a class="srclink rightside" href="../src/regex/re_unicode.rs.html#1407-1415">source</a><a href="#impl-Replacer-for-NoExpand%3C%27t%3E" class="anchor"></a><h3 class="code-header">impl&lt;'t&gt; <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>&lt;'t&gt;</h3></section><section id="impl-Replacer-for-F" class="impl has-srclink"><a class="srclink rightside" href="../src/regex/re_unicode.rs.html#1386-1394">source</a><a href="#impl-Replacer-for-F" class="anchor"></a><h3 class="code-header">impl&lt;F, T&gt; <a class="trait" href="trait.Replacer.html" title="trait regex::Replacer">Replacer</a> for F<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;F: FnMut(&amp;<a class="struct" href="struct.Captures.html" title="struct regex::Captures">Captures</a>&lt;'_&gt;) -&gt; T,<br>&nbsp;&nbsp;&nbsp;&nbsp;T: AsRef&lt;str&gt;,</span></h3></section></div><script src="../implementors/regex/re_unicode/trait.Replacer.js" data-ignore-extern-crates="core,alloc" async></script></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="regex" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>