Module show_notes::e019 [] [src]

Let's Clone a Cow!

Notes

Sometimes, we actually do need to copy types. Wouldn't it be nice if Rust gave us a convenient way to do that when it's convenient, or when the cost is low enough that the ergonomic tradeoffs are worth it? Well, perhaps unsurprisingly, it does! The Copy and Clone traits, plus the Cow type, give us everything we need!

Sponsors

(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)

Become a sponsor

Contact

Structs

A_NoCopyOrClonePoint

A non-copyable point type

B_JustClonePoint

A struct which implements Clone but not Copy.

C_CopyPoint

A struct with identical behavior to A_NoCopyOrClonePoint, except with Copy.

Functions

demonstrate_cow

The Cow type can wrap around other types and make them "reusable".

demonstrate_mut_cow

What if we need a mutable reference to the wrapped type?