Not dumb pointers.
- Date: June 17, 2016
Archave this in common: they're not dumb.
This episode, we take a close look at smart pointer types---from a few we've
already talked about, like
String, to some new ones,
- What smart pointers are, and what makes them 'smart'.
- Why we want or need smart pointers.
- A bit about
- A lot more about
Note: The examples below are in-progress: the
Rc example is complete
but not fully documented, and there's no examples yet for
will be! I expect to finish them over the course of this weekend, but I
wanted to go ahead and get the episode out!
- The Rust Programming Language:
- Rust by Example: 17.1: Box, stack, and heap
- API docs:
- 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.)
- 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
- 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!)
- New Rustacean:
- Chris Krycho
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
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
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
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
Arc, as those are the most
interesting bits from today's episode.
A trivial (and frankly rather silly) example for use with
Demonstrate the basics of reference-counted types. (Read the source, Luke!)
Note that this takes ownership of the data.
Note that this function is generic: it will work for any type.