Traits For Sharing
Deref, DerefMut
Used to transfer a
*
dereference of a type to some type in the inner structureCanonical cases are
Box
and similar containers
AsRef, AsMut
Annoying Rust shorthand: "refmut" is usually just spelled "mut"
Traits for borrowing a reference to your type from a variety of values
Usually used for generic parameters. Book says
fn open<P: AsRef<Path>>(path: P) -> ... { let path: &Path = path.as_ref() ...
Borrow, BorrowMut
Identical to
AsRef
,AsMut
?!?By convention, implement for a type only when references are semi-interchangeable with values
Mostly for collection type convenience
This is where the types start to get truly ugly: see the book example
From, Into
Type conversion traits
Not really needed, but convenient convention
Into
is justFrom
with the types reversedNormally just implement
From
to get a default implementation ofInto
from()
andinto()
consume their argumentsfrom()
andinto()
can only fail bypanic()
TryFrom, TryInto
Recent Rust addition
Like
From
andInto
but return aResult
instead of panickingMakes it possible to safely convert types when some of the values may not safely convert
ToOwned
Trait for "cloning" a thing that implements the
Borrow
traitOne standard way to create a
String
from an&str
:"hello".to_owned()
Also works for slice to
Vec
Cow
"Copy On Write": keeps a reference until owned
Glory in the beauty of this: book says
enum Cow<'a, B: ?Sized + 'a> where B: ToOwned { Borrowed(&'a B), Owned(<B as ToOwned>::Owned), }
Observations
There's a lot of stuff here
Read this chapter carefully, then…
Return to it as you advance in Rust. The details are best appreciated in the context of a problem you are trying to solve
The result of this mess is a pretty expressive language: much is "hidden under the hood"