Skip to content

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

Essays and write-ups

The compiler's own test cases

  • src/test/ui/traits at 673d0db — 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 to tests/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 blanket impl<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 turns Progress<Iter> into Progress<Iter, Bound>, tagged Unbounded or Bounded. with_bound() is written where Iter: ExactSizeIterator, because there is no sensible percentage of (0..); with_delims() is implemented only for Progress<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:

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, understanding From, implementing From (twice), and AsRef
  • 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