mirror of
https://github.com/esp-rs/esp-idf-hal.git
synced 2025-12-30 13:11:08 +00:00
60 lines
6.5 KiB
HTML
60 lines
6.5 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="Replaces all the instances of `$pattern` in `$input` (a `&'static str` constant) with `$replace_with` (a `&'static str` constant)."><title>str_replace in const_format - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.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-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-46132b98.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="const_format" data-themes="" data-resource-suffix="" data-rustdoc-version="1.86.0-nightly (8361aef0d 2025-01-14)" data-channel="nightly" data-search-js="search-75f5ac3e.js" data-settings-js="settings-0f613d39.js" ><script src="../static.files/storage-59e33391.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-893ab5e7.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-6580c154.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc macro"><!--[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="../const_format/index.html">const_<wbr>format</a><span class="version">0.2.34</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">str_<wbr>replace</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#signature" title="Signature">Signature</a></li><li><a href="#example" title="Example">Example</a></li></ul></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate const_<wbr>format</a></h2></div></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"><span class="rustdoc-breadcrumbs"><a href="index.html">const_format</a></span><h1>Macro <span class="macro">str_replace</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/const_format/macros/str_methods.rs.html#67-83">Source</a> </span></div><pre class="rust item-decl"><code>macro_rules! str_replace {
|
|
($input:expr, $pattern:expr, $replace_with:expr $(,)*) => { ... };
|
|
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Replaces all the instances of <code>$pattern</code> in <code>$input</code>
|
|
(a <code>&'static str</code> constant) with <code>$replace_with</code> (a <code>&'static str</code> constant).</p>
|
|
<h2 id="signature"><a class="doc-anchor" href="#signature">§</a>Signature</h2>
|
|
<p>This macro acts like a function of this signature:</p>
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">fn </span>str_replace(
|
|
input: <span class="kw-2">&</span><span class="lifetime">'static </span>str,
|
|
pattern: <span class="kw">impl </span>Pattern,
|
|
replace_with: <span class="kw-2">&</span><span class="lifetime">'static </span>str,
|
|
) -> <span class="kw-2">&</span><span class="lifetime">'static </span>str</code></pre></div>
|
|
<p>and is evaluated at compile-time.</p>
|
|
<p>Where <code>pattern</code> can be any of these types:</p>
|
|
<ul>
|
|
<li>
|
|
<p><code>&'static str</code></p>
|
|
</li>
|
|
<li>
|
|
<p><code>char</code></p>
|
|
</li>
|
|
<li>
|
|
<p><code>u8</code>: required to be ascii (<code>0</code> up to <code>127</code> inclusive).</p>
|
|
</li>
|
|
</ul>
|
|
<h2 id="example"><a class="doc-anchor" href="#example">§</a>Example</h2>
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>const_format::str_replace;
|
|
|
|
<span class="comment">// Passing a string pattern
|
|
</span><span class="macro">assert_eq!</span>(
|
|
<span class="macro">str_replace!</span>(<span class="string">"The incredible shrinking man."</span>, <span class="string">"i"</span>, <span class="string">"eee"</span>),
|
|
<span class="string">"The eeencredeeeble shreeenkeeeng man."</span>,
|
|
);
|
|
|
|
<span class="comment">// Passing a char pattern
|
|
</span><span class="macro">assert_eq!</span>(
|
|
<span class="macro">str_replace!</span>(<span class="string">"The incredible shrinking man."</span>, <span class="string">' '</span>, <span class="string">"---"</span>),
|
|
<span class="string">"The---incredible---shrinking---man."</span>,
|
|
);
|
|
|
|
<span class="comment">// Passing an ascii u8 pattern.
|
|
</span><span class="macro">assert_eq!</span>(
|
|
<span class="macro">str_replace!</span>(<span class="string">"The incredible shrinking man."</span>, <span class="string">b'i'</span>, <span class="string">"eee"</span>),
|
|
<span class="string">"The eeencredeeeble shreeenkeeeng man."</span>,
|
|
);
|
|
|
|
<span class="comment">// Removing all instances of the pattern
|
|
</span><span class="macro">assert_eq!</span>(
|
|
<span class="macro">str_replace!</span>(<span class="string">"remove haire"</span>, <span class="string">"re"</span>, <span class="string">""</span>),
|
|
<span class="string">"move hai"</span>,
|
|
);
|
|
|
|
<span class="comment">// This shows that all the arguments can be `const`s, they don't have to be literals.
|
|
</span>{
|
|
<span class="kw">const </span>IN: <span class="kw-2">&</span>str = <span class="string">"Foo Boo Patoo"</span>;
|
|
<span class="kw">const </span>REPLACING: <span class="kw-2">&</span>str = <span class="string">"oo"</span>;
|
|
<span class="kw">const </span>REPLACE_WITH: <span class="kw-2">&</span>str = <span class="string">"uh"</span>;
|
|
<span class="macro">assert_eq!</span>(<span class="macro">str_replace!</span>(IN, REPLACING, REPLACE_WITH), <span class="string">"Fuh Buh Patuh"</span>);
|
|
}</code></pre></div>
|
|
</div></details></section></div></main></body></html> |