Trait Garbage Bag
- There are a bunch of traits tied into Rust's internals or standard library with no real organizing principle
Clonetrait provides the
clone_into()is a good idea but little-used
Cloneimplementation should do a "deep copy"
Cloneis usually derived, but see e.g.
"Marker traits" are a communication channel between compiled code and the compiler
You can use a marker trait like any other trait
Marker traits can be ignored, e.g.
Copyis a marker trait that you implement when you want your values to be automatically copied by the compiler. It has no methods
Clonefor free as a subtrait
- Makes the implementation be careful
- Semantics sometimes surprising
You can implement the
Droptrait to get control of a value right before it is freed
This is used for e.g. closing files, flushing data, etc
A type implementing
Copycannot also implement
Drop, because the semantics are too confusing
Sizedis a marker trait that says that the compiler knows the size of values of the type
You cannot implement
By default, generic types implicitly require instantiation with something
You can turn this off with
+ ?Sized("questionably sized") in situations where you don't want it
Trait to provide a "default value" for your type
Perhaps a bad idea: what does "default value" even mean?
… But often convenient
Book provides a sketchy use case
These days, Clippy tries to insist