Traits, generics and macros
Rust has no inheritance: shared behaviour is a trait, and a type that implements it says so. These fourteen lessons write a trait with default methods, then the bounds, the where clause and the associated type that make one function work for many types, and the dyn Trait that chooses the implementation at run time. The derived traits, operator traits and composition follow, then the macros that remove the typing: macro_rules!, its repetition, and a procedural macro. Closures and iterators close the chapter, because the adaptors of the next one are built on them.
035
Traits and default methods
a trait as shared behaviour, implementing it for two types, default methods and a trait bound (C++ 036)
Open
036
dyn Trait and dynamic dispatch
&dyn and Box<dyn>, the vtable, object safety and a vector of different shapes (C++ 035)
Open
037
Generic functions and structs
a type parameter, monomorphisation, a generic struct, and a generic function that works for many types (C++ 037, 038)
Open
038
Bounds, where clauses and impl Trait
T: Trait, several bounds, where clauses, impl Trait in arguments and returns, and static dispatch (C++ 043, 044, 046, 047)
Open
039
Associated types and type aliases
an associated type in a trait, its use in a signature, type aliases and the difference from a type parameter (C++ 039, 042, 045)
Open
040
Operator traits: Add, Index, Display
std::ops::Add, Index, PartialEq and Display on a value type, and the rules that keep them predictable (C++ 030)
Open
041
derive and marker traits
#[derive(Debug, Clone, PartialEq)], what the compiler writes, and marker traits such as Copy and Send (C++ 042)
Open
042
Composition instead of inheritance
no inheritance: a struct that owns another, a trait for the shared part, and a base class rewritten (C++ 034)
Open
043
Macros with macro_rules!
a declarative macro, its matcher and transcriber, hygiene, and a macro that builds a struct value (C++ 040)
Open
044
Macros that take many arguments
$( ... ),* repetition, a variadic macro, counting arguments and a macro that prints several values (C++ 040, 041)
Open
045
Procedural macros
a proc-macro crate, TokenStream in and out, a function-like macro and a derive macro (C++ 048)
Open
046
Closures and captures
|x| expressions, capture by reference and by move, Fn, FnMut and FnOnce, and a closure stored in a variable (C++ 052)
Open
047
The Iterator trait
next(), the trait implemented by hand, for as sugar over next, and a countdown iterator (C++ 049)
Open
048
map, filter, collect and chains
adaptors against consumers, lazy chains, collect into a Vec or a String, and a pipeline against a loop (C++ 050, 058)
Open