gh-pages
simonsan 2 years ago
parent 5534036d57
commit 42d55805e5

@ -139,7 +139,8 @@
<main> <main>
<h1 id="collections-are-smart-pointers"><a class="header" href="#collections-are-smart-pointers">Collections are smart pointers</a></h1> <h1 id="collections-are-smart-pointers"><a class="header" href="#collections-are-smart-pointers">Collections are smart pointers</a></h1>
<h2 id="description"><a class="header" href="#description">Description</a></h2> <h2 id="description"><a class="header" href="#description">Description</a></h2>
<p>Use the <code>Deref</code> trait to treat collections like smart pointers, offering owning <p>Use the <a href="https://doc.rust-lang.org/std/ops/trait.Deref.html"><code>Deref</code></a>
trait to treat collections like smart pointers, offering owning
and borrowed views of data.</p> and borrowed views of data.</p>
<h2 id="example"><a class="header" href="#example">Example</a></h2> <h2 id="example"><a class="header" href="#example">Example</a></h2>
<pre><code class="language-rust ignore">use std::ops::Deref; <pre><code class="language-rust ignore">use std::ops::Deref;
@ -157,16 +158,16 @@ impl&lt;T&gt; Deref for Vec&lt;T&gt; {
} }
} }
</code></pre> </code></pre>
<p>A <code>Vec&lt;T&gt;</code> is an owning collection of <code>T</code>s, a slice (<code>&amp;[T]</code>) is a borrowed <p>A <code>Vec&lt;T&gt;</code> is an owning collection of <code>T</code>s, while a slice (<code>&amp;[T]</code>) is a borrowed
collection of <code>T</code>s. Implementing <code>Deref</code> for <code>Vec</code> allows implicit dereferencing collection of <code>T</code>s. Implementing <code>Deref</code> for <code>Vec</code> allows implicit dereferencing
from <code>&amp;Vec&lt;T&gt;</code> to <code>&amp;[T]</code> and includes the relationship in auto-derefencing from <code>&amp;Vec&lt;T&gt;</code> to <code>&amp;[T]</code> and includes the relationship in auto-derefencing
searches. Most methods you might expect to be implemented for <code>Vec</code>s are instead searches. Most methods you might expect to be implemented for <code>Vec</code>s are instead
implemented for slices.</p> implemented for slices.</p>
<p>See also <code>String</code> and <code>&amp;str</code>.</p> <p>Also <code>String</code> and <code>&amp;str</code> have a similar relation.</p>
<h2 id="motivation"><a class="header" href="#motivation">Motivation</a></h2> <h2 id="motivation"><a class="header" href="#motivation">Motivation</a></h2>
<p>Ownership and borrowing are key aspects of the Rust language. Data structures <p>Ownership and borrowing are key aspects of the Rust language. Data structures
must account for these semantics properly in order to give a good user must account for these semantics properly to give a good user
experience. When implementing a data structure which owns its data, offering a experience. When implementing a data structure that owns its data, offering a
borrowed view of that data allows for more flexible APIs.</p> borrowed view of that data allows for more flexible APIs.</p>
<h2 id="advantages"><a class="header" href="#advantages">Advantages</a></h2> <h2 id="advantages"><a class="header" href="#advantages">Advantages</a></h2>
<p>Most methods can be implemented only for the borrowed view, they are then <p>Most methods can be implemented only for the borrowed view, they are then
@ -179,7 +180,7 @@ pattern can get complex (see the <code>Borrow</code> and <code>AsRef</code> trai
<h2 id="discussion"><a class="header" href="#discussion">Discussion</a></h2> <h2 id="discussion"><a class="header" href="#discussion">Discussion</a></h2>
<p>Smart pointers and collections are analogous: a smart pointer points to a single <p>Smart pointers and collections are analogous: a smart pointer points to a single
object, whereas a collection points to many objects. From the point of view of object, whereas a collection points to many objects. From the point of view of
the type system there is little difference between the two. A collection owns the type system, there is little difference between the two. A collection owns
its data if the only way to access each datum is via the collection and the its data if the only way to access each datum is via the collection and the
collection is responsible for deleting the data (even in cases of shared collection is responsible for deleting the data (even in cases of shared
ownership, some kind of borrowed view may be appropriate). If a collection owns ownership, some kind of borrowed view may be appropriate). If a collection owns
@ -193,8 +194,10 @@ implement <code>Deref&lt;Target=Bar&lt;T&gt;&gt;</code> where <code>Bar</code> i
<p>Commonly, ordered collections will implement <code>Index</code> for <code>Range</code>s to provide <p>Commonly, ordered collections will implement <code>Index</code> for <code>Range</code>s to provide
slicing syntax. The target will be the borrowed view.</p> slicing syntax. The target will be the borrowed view.</p>
<h2 id="see-also"><a class="header" href="#see-also">See also</a></h2> <h2 id="see-also"><a class="header" href="#see-also">See also</a></h2>
<p><a href="../anti_patterns/deref.html">Deref polymorphism anti-pattern</a>.</p> <ul>
<p><a href="https://doc.rust-lang.org/std/ops/trait.Deref.html">Documentation for <code>Deref</code> trait</a>.</p> <li><a href="../anti_patterns/deref.html">Deref polymorphism anti-pattern</a>.</li>
<li><a href="https://doc.rust-lang.org/std/ops/trait.Deref.html">Documentation for <code>Deref</code> trait</a>.</li>
</ul>
</main> </main>

@ -502,7 +502,8 @@ not be &quot;default&quot;</li>
</ul> </ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="collections-are-smart-pointers"><a class="header" href="#collections-are-smart-pointers">Collections are smart pointers</a></h1> <div style="break-before: page; page-break-before: always;"></div><h1 id="collections-are-smart-pointers"><a class="header" href="#collections-are-smart-pointers">Collections are smart pointers</a></h1>
<h2 id="description-4"><a class="header" href="#description-4">Description</a></h2> <h2 id="description-4"><a class="header" href="#description-4">Description</a></h2>
<p>Use the <code>Deref</code> trait to treat collections like smart pointers, offering owning <p>Use the <a href="https://doc.rust-lang.org/std/ops/trait.Deref.html"><code>Deref</code></a>
trait to treat collections like smart pointers, offering owning
and borrowed views of data.</p> and borrowed views of data.</p>
<h2 id="example-3"><a class="header" href="#example-3">Example</a></h2> <h2 id="example-3"><a class="header" href="#example-3">Example</a></h2>
<pre><code class="language-rust ignore">use std::ops::Deref; <pre><code class="language-rust ignore">use std::ops::Deref;
@ -520,16 +521,16 @@ impl&lt;T&gt; Deref for Vec&lt;T&gt; {
} }
} }
</code></pre> </code></pre>
<p>A <code>Vec&lt;T&gt;</code> is an owning collection of <code>T</code>s, a slice (<code>&amp;[T]</code>) is a borrowed <p>A <code>Vec&lt;T&gt;</code> is an owning collection of <code>T</code>s, while a slice (<code>&amp;[T]</code>) is a borrowed
collection of <code>T</code>s. Implementing <code>Deref</code> for <code>Vec</code> allows implicit dereferencing collection of <code>T</code>s. Implementing <code>Deref</code> for <code>Vec</code> allows implicit dereferencing
from <code>&amp;Vec&lt;T&gt;</code> to <code>&amp;[T]</code> and includes the relationship in auto-derefencing from <code>&amp;Vec&lt;T&gt;</code> to <code>&amp;[T]</code> and includes the relationship in auto-derefencing
searches. Most methods you might expect to be implemented for <code>Vec</code>s are instead searches. Most methods you might expect to be implemented for <code>Vec</code>s are instead
implemented for slices.</p> implemented for slices.</p>
<p>See also <code>String</code> and <code>&amp;str</code>.</p> <p>Also <code>String</code> and <code>&amp;str</code> have a similar relation.</p>
<h2 id="motivation"><a class="header" href="#motivation">Motivation</a></h2> <h2 id="motivation"><a class="header" href="#motivation">Motivation</a></h2>
<p>Ownership and borrowing are key aspects of the Rust language. Data structures <p>Ownership and borrowing are key aspects of the Rust language. Data structures
must account for these semantics properly in order to give a good user must account for these semantics properly to give a good user
experience. When implementing a data structure which owns its data, offering a experience. When implementing a data structure that owns its data, offering a
borrowed view of that data allows for more flexible APIs.</p> borrowed view of that data allows for more flexible APIs.</p>
<h2 id="advantages-1"><a class="header" href="#advantages-1">Advantages</a></h2> <h2 id="advantages-1"><a class="header" href="#advantages-1">Advantages</a></h2>
<p>Most methods can be implemented only for the borrowed view, they are then <p>Most methods can be implemented only for the borrowed view, they are then
@ -542,7 +543,7 @@ pattern can get complex (see the <code>Borrow</code> and <code>AsRef</code> trai
<h2 id="discussion"><a class="header" href="#discussion">Discussion</a></h2> <h2 id="discussion"><a class="header" href="#discussion">Discussion</a></h2>
<p>Smart pointers and collections are analogous: a smart pointer points to a single <p>Smart pointers and collections are analogous: a smart pointer points to a single
object, whereas a collection points to many objects. From the point of view of object, whereas a collection points to many objects. From the point of view of
the type system there is little difference between the two. A collection owns the type system, there is little difference between the two. A collection owns
its data if the only way to access each datum is via the collection and the its data if the only way to access each datum is via the collection and the
collection is responsible for deleting the data (even in cases of shared collection is responsible for deleting the data (even in cases of shared
ownership, some kind of borrowed view may be appropriate). If a collection owns ownership, some kind of borrowed view may be appropriate). If a collection owns
@ -556,8 +557,10 @@ implement <code>Deref&lt;Target=Bar&lt;T&gt;&gt;</code> where <code>Bar</code> i
<p>Commonly, ordered collections will implement <code>Index</code> for <code>Range</code>s to provide <p>Commonly, ordered collections will implement <code>Index</code> for <code>Range</code>s to provide
slicing syntax. The target will be the borrowed view.</p> slicing syntax. The target will be the borrowed view.</p>
<h2 id="see-also-3"><a class="header" href="#see-also-3">See also</a></h2> <h2 id="see-also-3"><a class="header" href="#see-also-3">See also</a></h2>
<p><a href="idioms/../anti_patterns/deref.html">Deref polymorphism anti-pattern</a>.</p> <ul>
<p><a href="https://doc.rust-lang.org/std/ops/trait.Deref.html">Documentation for <code>Deref</code> trait</a>.</p> <li><a href="idioms/../anti_patterns/deref.html">Deref polymorphism anti-pattern</a>.</li>
<li><a href="https://doc.rust-lang.org/std/ops/trait.Deref.html">Documentation for <code>Deref</code> trait</a>.</li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="finalisation-in-destructors"><a class="header" href="#finalisation-in-destructors">Finalisation in destructors</a></h1> <div style="break-before: page; page-break-before: always;"></div><h1 id="finalisation-in-destructors"><a class="header" href="#finalisation-in-destructors">Finalisation in destructors</a></h1>
<h2 id="description-5"><a class="header" href="#description-5">Description</a></h2> <h2 id="description-5"><a class="header" href="#description-5">Description</a></h2>
<p>Rust does not provide the equivalent to <code>finally</code> blocks - code that will be <p>Rust does not provide the equivalent to <code>finally</code> blocks - code that will be

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