|
|
@ -502,7 +502,8 @@ not be "default"</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<T> Deref for Vec<T> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</code></pre>
|
|
|
|
</code></pre>
|
|
|
|
<p>A <code>Vec<T></code> is an owning collection of <code>T</code>s, a slice (<code>&[T]</code>) is a borrowed
|
|
|
|
<p>A <code>Vec<T></code> is an owning collection of <code>T</code>s, while a slice (<code>&[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>&Vec<T></code> to <code>&[T]</code> and includes the relationship in auto-derefencing
|
|
|
|
from <code>&Vec<T></code> to <code>&[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>&str</code>.</p>
|
|
|
|
<p>Also <code>String</code> and <code>&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<Target=Bar<T>></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
|
|
|
|