My second book about Rust is now in print: Rust Brain Teasers. It's available in paperback and e-book from The Pragmatic Publisher, worldwide book distributors and your local library/favorite bookseller (ask for ISBN 978-1680509175).
The source code used in the book is in this Github repo.
As my first book, Hands-on Rust was approaching completion I was approached by PragProg to write a book for their new "brain teasers" line. The concept was intriguing: short code snippets, asking the reader to guess the result---followed by a discussion as to why the result might not be what the reader would expect. I was attracted to this as a learning technique: apparent "gotchas" are often implemented in a certain way for a good reason. Understanding the underlying reason would really help the reader broaden their language knowledge.
So I sat down, and started writing out unexpected results I'd run into when using Rust. This was actually pretty tough---Rust is a very consistent language, and Clippy does a great job of warning you when you aren't doing something as intended. I came up with a pretty big list in the end, and whittled it down to 23 that were worth writing about---and one just for fun. I focused on issues that beginner-to-intermediate readers are likely to encounter; if you're on the Rust Core Team, you probably know more about these topics than I do! I also tried to cover things that people have asked me about.
At 138 pages, it's a relatively short book---and consequently pretty inexpensive.
- Floating-point precision
- Trimming strings/input
- Precision loss with
- Wrapping/Numeric Overflow
- String length in a UTF-8 world
- Auto-Magical implementation of
- The perils of using
as _for fake duck typing, which branches into generics
- Sorting floating point (and other
- Homoglyphs (Unicode letters that look far too similar, and Unicode normalization in general)
- Overflowing your stack with
Box::newand a large array
- Deliberately leaking memory with
- Structure size and padding with
- Unprintable structures due to
- C++-style function shadowing (multiple functions of the same name with different parameter types, which leads to more generics)
- Vector growth factor
- Mutable content of an immutable variable
- Blocking in an async context
- Dynamic vs. static dispatch
- Un-droppable structures that reference themselves
- Constant loops (why you can't use
forin a constant function)
The release announcement was delayed a bit because we adopted a lovely baby girl (now nearly 4 months old!), and I took as much paternity leave as I could.