Rust Brain Teasers - now in print

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.

Topics covered:

  • Floating-point precision
  • Trimming strings/input
  • Precision loss with as
  • Wrapping/Numeric Overflow
  • String length in a UTF-8 world
  • Auto-Magical implementation of into from from
  • The perils of using as _ for fake duck typing, which branches into generics
  • Sorting floating point (and other partial_ord types) dat
  • Homoglyphs (Unicode letters that look far too similar, and Unicode normalization in general)
  • Overflowing your stack with Box::new and a large array
  • Deliberately leaking memory with std::forget
  • Shadowing
  • Structure size and padding with repr
  • Unprintable structures due to RefCell loops.
  • 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 for in 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. :slight_smile:

5 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.