Traits: links and videos¶
Level: reference · the reading list
One line: The sources behind this section — the normative ones first, then the essays worth the detour, then the videos.
The same page for strings is Strings: links, books and videos — the two sections lean on each other, and ToOwned and Display are why.
Official¶
- The Reference — Items: traits ↗ — the normative definition. Terse, and the only one of these that is binding: when two explanations disagree, this is the one that is right.
- The Book, ch. 10 — Traits: defining shared behaviour ↗ — declaration,
impl … for, default method bodies, trait bounds,where. - The Book, ch. 17 — Trait objects ↗ —
dyn, the vtable, and dynamic dispatch. - The Book, ch. 19 — the newtype pattern for external traits on external types ↗ — the sanctioned way round the orphan rule.
- Rust by Example — traits ↗ — the smallest runnable version of the idea.
stdkeyword docs —dyn↗ — the two pointers adyn Traitreference carries, the object-safety requirement, and the edition differences (before 2021 the keyword could be omitted; in 2015 a leading::was parsed as part of the path).- Edition guide —
dyn Traitfor trait objects ↗ — why the keyword exists at all: bareTraitas a type was ambiguous, and 2018 made the dynamic case say so. - Announcing
impl Traitcapture rules (2024) ↗ — what changed in edition 2024 about which lifetimes animpl Traitreturn captures.
Essays and write-ups¶
- Traits: a blog post by Aaron Turon (2015) ↗ — the design rationale from the person who built it: why traits rather than interfaces, what coherence buys, and where the zero-cost claim comes from. Old, and still the best single explanation of why.
dyn Trait— quinedot's Rust learning notes ↗ — the long, careful treatment of trait objects: lifetimes, object safety, what a vtable actually contains, and the cases the Book skips. The best single page ondynanywhere.- Rust traits: a deep dive ↗ — long-form tour of the whole surface.
- What are traits in Rust? ↗ — a short answer page, useful as a refresher.
- A half-hour to learn Rust ↗ — not a traits article; the whirlwind tour that puts traits in context with everything around them.
- Easy Rust — ch. 34, traits ↗ — the written companion to the Easy Rust videos below.
- When Rust hurts ↗ — the critical piece: where the trait system makes a design harder rather than easier. Worth reading precisely because it is not advocacy.
- Felix Knorr — traits (2023) ↗
- My Rust journey: traits ↗ — one person's write-up.
- Pythonista to Rustacean, part 1 ↗ — flagged as unclear when it was collected; read it as a lead, not as an authority.
- Advanced traits — practice.rs ↗ — exercises rather than prose.
The compiler's own test cases¶
src/test/ui/traitsat673d0db↗ — every trait behaviour rustc guarantees, written as the smallest program that exercises it, each with its expected diagnostic beside it. The best answer to "does this compile, and what exactly does it say when it doesn't". Note the URL is pinned to a commit on purpose: the directory has since moved totests/ui/traits, so the pin is what keeps the link alive.
Video¶
- Type-Driven API Design in Rust ↗ — Will Crichton, Strange Loop 2021, ~41 min. The one to watch once the pages above have landed and the question turns from what is a trait into what are they for. It is live-coded from an empty file, and the whole talk is a single example: a progress bar. It starts as a
println!inside a loop, becomes a function, then a generic function, then an extension trait — a trait declared locally and given a blanketimpl<Iter> ProgressIteratorExt for Iter, which is what puts a.progress()method on types nobody here owns. Narrowing that blanket impl so it applies to iterators and nothing else is its own step, and the talk names it. Then the payoff: a second type parameter turnsProgress<Iter>intoProgress<Iter, Bound>, taggedUnboundedorBounded.with_bound()is writtenwhere Iter: ExactSizeIterator, because there is no sensible percentage of(0..);with_delims()is implemented only forProgress<Iter, Bounded>, so the bracket characters are unreachable until there is a bar to put them around. Nothing is checked at run time and no method returns an error — the calls that would have had to fail are simply not there. That is typestate, arrived at rather than announced, and watching it arrive is what a written explanation cannot do.
Three things worth taking with it:
- The written sequel, by the same author: Type-Driven API Design in Rust ↗, a short free book and not a transcript. It states the idea the talk only performs — an API is a representation ↗, and a representation makes some tasks easy and others hard, which is a claim from cognitive science and is cited as one — then works through witnesses ↗ and guards ↗, typestate ↗ and state combinators ↗, parallel lists ↗ and registries ↗. It assumes TRPL and nothing past it. Its access-control chapter is the pattern that The right to post is a value builds in full.
- The code, transcribed by a viewer at fbrisa/type-driven-api-design-in-rust ↗ — one commit per step, so
git log --reversereads as the talk's outline: basic progress bar → with iterator → implementing Trait → constraining ProgressIteratorExt to only iterators → Bounded, Unbounded. It is not the author's repository, so read it as somebody's notes rather than as errata. - Who is speaking, which explains the talk's shape: Crichton went on to build the Brown University edition of The Book that this library sends you to for chapter 4. The quizzes there and the API design here come from one research question — what do programmers actually get wrong, and can the compiler be made to catch it for them.
One older one, also with a URL:
The rest are offline copies, listed by title so they can be found again:
- Rust traits vs C++ concepts
- Designing interfaces — Rust for Rustaceans book club, ch. 3
- Demo: traits [34 of 35] — Rust for Beginners
- Method overloading (kinda), and advanced trait usage — Quick Rust
- Easy Rust 063–070, 072–073 — the traits arc: introduction, reading an implementation, implementing
Display, a longer trait, traits built on other traits, trait bounds, understandingFrom, implementingFrom(twice), andAsRef - Easy Rust 071 — type aliases and new types — sits inside that run but is a different topic; the newtype half is what makes it belong (a score is not a number covers the same ground)
Po polsku¶
Ta strona jest listą źródeł, a przy cechach (traits) trzeba powiedzieć wprost, dlaczego wszystkie są angielskie: Tour of Rust ↗, jedyny obszerny polski kurs Rusta, jest przetłumaczony do rozdziału 5, a cechy zaczynają się w rozdziale 7. Polskie wpisy blogowe o traitach istnieją, ale są krótkie i prawie nigdy nie dochodzą do tego, co naprawdę boli — do obiektów cech (trait objects), reguły sieroty (orphan rule) i tego, co konkretnie siedzi w tablicy metod wirtualnych. Ta lista nie jest angielska z niedbalstwa; innej po prostu nie ma.
Warto też zauważyć, że kolejność na tej stronie jest hierarchią, a nie kaprysem redakcyjnym. The Reference jest wiążący: kiedy Book, esej i wpis na blogu mówią co innego, rację ma Reference. Book uczy, eseje tłumaczą „dlaczego”, a katalog testów kompilatora odpowiada na pytanie „czy to się w ogóle skompiluje i co dokładnie wypisze, gdy nie” — dlatego link do niego jest przypięty do konkretnego commita, bo katalog zdążył się przenieść. Jeśli masz przeczytać tylko dwie pozycje z tej listy, weź esej Aarona Turona z 2015 roku (skąd cechy zamiast interfejsów i co daje spójność) oraz stronę quinedota o dyn Trait — po polsku odpowiednika żadnej z nich nie ma.
Szukaj po polsku: cechy w Ruscie · obiekt cechy · rust trait object dyn · rust orphan rule coherence · rust trait vtable