Module show_notes::e018
source · Expand description
Borrow
, AsRef
, Deref
: my head hurts now
- Date: January 31, 2017
- Subject: Three traits which are essential for designing good, Rustic APIs.
- Audio
§Notes
Borrow
, AsRef
, and Deref
are a little complicated, but they’re
well-worth understanding. Together, they give you tools for dealing with
everything from HashMap
and friends to conversions involving smart pointer
types to easily using String
and str
or Vec
and slice together.
§Links
AsRef
,Borrow
, andDeref
:Borrow
andAsRef
in The Rust Programming Languagecollections::borrow::Borrow
std::convert::AsRef
std::ops::Deref
- persistent data structures
- “Rust and Rest” – Arnin Roacher
- sentry-cli
- Particularly relevant previous episodes:
§Sponsors
- Aleksey Pirogov
- Andreas Fischer
- Andrew Thompson
- Ben Whitley
- Cameron Mochrie
- Chris Palmer
- Christopher Giffard
- Daniel Collin
- Derek Morr
- Jakub “Limeth” Hlusička
- Jordan Henderson
- Jupp Müller
- Keith Gray
- Lachlan Collins
- Luca Schmid
- Matt Rudder
- Matthew Piziak
- Micael Bergeron
- Ovidiu Curcan
- Pascal Hertleif
- Peter Tillemans
- Philipp Keller
- Ralph Giles (“rillian”)
- Raph Levien
- reddraggone9
- Ryan Ollos
- Steven Murawski
- Vesa Kaihlavirta
- Vlad Bezden
- William Roe
- Zaki
(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
Structs§
- Demonstrate borrowing the internal contents of the item.
- A struct for showing that you cannot use
Borrow
,AsRef
Functions§
- Take the same type by
Deref
coercion at the call site. - Take it implementing
Borrow<[u8]>
. - Take it implementing
AsRef<[u8]>
. Note similarity totakes_a_borrowable
.