gh-pages
Dhghomon 4 years ago
parent adaa1e7503
commit ea7ef07208

@ -181,7 +181,7 @@ fn main() {}
<p>Next are the <code>enum</code> variants. They are <code>Borrowed</code> and <code>Owned</code>.</p>
<p>Imagine that you have a function that returns <code>Cow&lt;'static, str&gt;</code>. If you tell the function to return <code>&quot;My message&quot;.into()</code>, it will look at the type: &quot;My message&quot; is a <code>str</code>. This is a <code>Borrowed</code> type, so it chooses <code>Borrowed(&amp;'a B)</code>. So it becomes <code>Cow::Borrowed(&amp;'static str)</code>.</p>
<p>And if you give it a <code>format!(&quot;{}&quot;, &quot;My message&quot;).into()</code> then it will look at the type. This time it is a <code>String</code>, because <code>format!</code> makes a <code>String</code>. So this time it will select &quot;Owned&quot;.</p>
<p>Here is an example to test <code>Cow</code>. We will put a number into a function that returns a <code>Cow&lt;'static, str&gt;</code>. Depending on the number, it will create a <code>&amp;str</code> or a <code>String</code>. Then it uses <code>.into()</code> to turn it into a <code>Cow</code>. When you do that, it will choose either <code>Cow:::Borrowed</code> or <code>Cow::Owned</code>. Then we will match to see which one it chose.</p>
<p>Here is an example to test <code>Cow</code>. We will put a number into a function that returns a <code>Cow&lt;'static, str&gt;</code>. Depending on the number, it will create a <code>&amp;str</code> or a <code>String</code>. Then it uses <code>.into()</code> to turn it into a <code>Cow</code>. When you do that, it will choose either <code>Cow::Borrowed</code> or <code>Cow::Owned</code>. Then we will match to see which one it chose.</p>
<pre><pre class="playground"><code class="language-rust">use std::borrow::Cow;
fn modulo_3(input: u8) -&gt; Cow&lt;'static, str&gt; {

@ -6303,7 +6303,7 @@ fn main() {}
<p>Next are the <code>enum</code> variants. They are <code>Borrowed</code> and <code>Owned</code>.</p>
<p>Imagine that you have a function that returns <code>Cow&lt;'static, str&gt;</code>. If you tell the function to return <code>&quot;My message&quot;.into()</code>, it will look at the type: &quot;My message&quot; is a <code>str</code>. This is a <code>Borrowed</code> type, so it chooses <code>Borrowed(&amp;'a B)</code>. So it becomes <code>Cow::Borrowed(&amp;'static str)</code>.</p>
<p>And if you give it a <code>format!(&quot;{}&quot;, &quot;My message&quot;).into()</code> then it will look at the type. This time it is a <code>String</code>, because <code>format!</code> makes a <code>String</code>. So this time it will select &quot;Owned&quot;.</p>
<p>Here is an example to test <code>Cow</code>. We will put a number into a function that returns a <code>Cow&lt;'static, str&gt;</code>. Depending on the number, it will create a <code>&amp;str</code> or a <code>String</code>. Then it uses <code>.into()</code> to turn it into a <code>Cow</code>. When you do that, it will choose either <code>Cow:::Borrowed</code> or <code>Cow::Owned</code>. Then we will match to see which one it chose.</p>
<p>Here is an example to test <code>Cow</code>. We will put a number into a function that returns a <code>Cow&lt;'static, str&gt;</code>. Depending on the number, it will create a <code>&amp;str</code> or a <code>String</code>. Then it uses <code>.into()</code> to turn it into a <code>Cow</code>. When you do that, it will choose either <code>Cow::Borrowed</code> or <code>Cow::Owned</code>. Then we will match to see which one it chose.</p>
<pre><pre class="playground"><code class="language-rust">use std::borrow::Cow;
fn modulo_3(input: u8) -&gt; Cow&lt;'static, str&gt; {

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save