Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Appendix C — Trait Quick Reference

Trait Map

Standard Traits by Capability Family

TypeDebug / DisplayClone / Copy / DefaultIterator / IntoIteratorFrom / TryFrom / AsRefSend / Sync / UnpinEq / Ord / Hash
TraitWhat it meansDerivable?When manual impl is necessaryCommon mistake
DebugType can be formatted for debuggingUsually yesCustom debug structure or redactionConfusing debug output with user-facing formatting
DisplayType has a user-facing textual formNoAlmost always manualUsing Debug where Display is expected
CloneType can produce an explicit duplicateOften yesCustom deep-copy or handle semanticsTreating clone() as always cheap
CopyType can be duplicated by plain bit-copyOften yes if eligibleRarely, because rules are strictTrying to make a type Copy when it has ownership or Drop
DefaultType has a canonical default constructorOften yesDefaults depend on invariants or smart constructorsGiving a meaningless default that violates domain clarity
PartialEqValues can be compared for equalityOften yesFloating rules or custom semanticsDeriving equality when identity semantics differ
EqEquality is total and reflexiveOften yesRare; usually paired with PartialEqImplementing for NaN-like semantics where reflexivity fails
PartialOrdValues have a partial orderingOften yesDomain-specific ordering logicAssuming partial order is total
OrdValues have a total orderingOften yesManual canonical order neededImplementing an order inconsistent with Eq
HashType can be hashed consistently with equalityOften yesCanonicalization or subset hashingHash not matching equality semantics
From<T>Infallible conversion from TNoCustom conversion rulesPutting fallible conversion here instead of TryFrom
TryFrom<T>Fallible conversion from TNoValidation is requiredHiding validation failure with panics
AsRef<T>Cheap borrowed view into another typeNoBoundary APIs and adaptersReturning owned values instead of views
Borrow<T>Hash/ordering-compatible borrowed formNoCollections and map lookupsImplementing when borrowed and owned forms are not semantically identical
DerefSmart-pointer-like transparent accessNoPointer wrappersUsing Deref for unrelated convenience conversions
IteratorProduces a sequence of items via next()NoCustom iteration behaviorForgetting that iterators are lazy until consumed
IntoIteratorType can be turned into an iteratorOften indirectlyCollections and custom containersMissing owned/reference iterator variants
ErrorStandard error trait for failure typesNoLibrary/application error typesExposing String where a structured error is needed
SendSafe to transfer ownership across threadsAuto traitManual unsafe impl only for proven-safe abstractionsAssuming Send is about mutability instead of thread transfer
SyncSafe for &T to be shared across threadsAuto traitManual unsafe impl only with strong invariantsConfusing Sync with “internally immutable”
UnpinSafe to move after pinning contextsAuto traitSelf-referential or movement-sensitive typesTreating Pin/Unpin as async-only instead of movement semantics

Traits You Will See Constantly

CategoryTraits you should recognize instantly
FormattingDebug, Display
Ownership/value behaviorClone, Copy, Drop, Default
Equality and orderingPartialEq, Eq, PartialOrd, Ord, Hash
Conversion and borrowingFrom, TryFrom, AsRef, Borrow, Deref
IterationIterator, IntoIterator
ErrorsError
ConcurrencySend, Sync
Async movementUnpin