Module show_notes::e015
source · Expand description
Not dumb pointers.
- Date: June 17, 2016
- Subject:
Box
,String
,Vec
,Rc
, andArc
have this in common: they’re not dumb. - Audio
§Notes
This episode, we take a close look at smart pointer types—from a few we’ve
already talked about, like Box
, Vec
, and String
, to some new ones,
like Rc
and Arc
.
- What smart pointers are, and what makes them ‘smart’.
- Why we want or need smart pointers.
- A bit about
Box
. - A lot more about
Rc
andArc
.
Note: The examples below are in-progress: the Rc
example is complete
but not fully documented, and there’s no examples yet for Arc
—but there
will be! I expect to finish them over the course of this weekend, but I
wanted to go ahead and get the episode out!
§Further reading
- The Rust Programming Language:
- The Stack and the Heap
- Choosing Your Guarantees – see especially the sections on
Rc
andArc
.
- Rust by Example: 17.1: Box, stack, and heap
- API docs:
§Links
- RustConf
- Rust Belt Rust Conference
- Rusty Radio
- Rust Exercism track
- RFC 1636: Require documentation for all new features. (Note: I misspoke on the episode and said this was at rust-lang.org; it’s not! It’s on GitHub, wtih the rest of the RFCs, of course.)
§Sponsors
- Aleksey Pirogov
- Chris Palmer
- Daniel Collin
- Derek Morr
- Doug Reeves
- Hamza Sheikh
- Lachlan Collins
- Leif Arne Storset
- Luca Schmid
- Micael Bergeron
- Pascal Hertleif
- Ralph Giles (“rillian”)
- Ralph “FriarTech” Loizzo
- Raph Levien
- reddraggone9
- Ryan Ollos
- Vesa Kaihlavirta
- William Roe
(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)
§Become a sponsor
§Contact
- New Rustacean:
- Twitter: @newrustacean
- Email: hello@newrustacean.com
- Chris Krycho
- GitHub: chriskrycho
- Twitter: @chriskrycho
§Examples
The most basic examples of smart pointers involve the Box
type, which
we’ve talked about before. Assume we had a type Foo
which took a string in
its constructor, and that we wanted to box it up. We would just write:
let someFoo = Box::new(Foo::new("bar"));
It’s also worth comparing the Rust code above with similar code in C++.
Assume we have a class
with the same name; using a smart pointer (in this
case, unique_ptr
, returned from make_unique
) would give us this code:
const auto someFoo = std::make_unique<const Foo>("bar");
Both examples declare a smart pointer named someFoo
that points to an
immutable/constant Foo
and where the pointer itself is immutable/constant.
However, note that the Rust code is briefer and (at least in my opinion)
substantially clearer than the corresponding C++ code to express the same
semantic content.
I’m not including further comments on Box
here in the docs, because we’ve
covered it before and it’s fairly straightforward. The rest of these
materials focus entirely on Rc
and Arc
, as those are the most
interesting bits from today’s episode.
Structs§
- A trivial (and frankly rather silly) example for use with
Rc
.
Functions§
- Demonstrate the basics of reference-counted types. (Read the source, Luke!)
- Note that this takes ownership of the data.
- Note that this function is not generic because it assumes
FileData
. - Note that this function is generic: it will work for any type.