REVIEW - The Rust Programming Language, 2nd Edition


Title:

The Rust Programming Language, 2nd Edition

Author:

Steve Klabnik, Carol Nichols

Publisher:

No Starch Press (2022)

Pages:

560

Reviewer:

Pete Cordell

Reviewed:

March 2024

Rating:

★★★☆☆


Verdict: Recommended with reservations.

Firstly, I need to make an apology to Ian. I got this book in February but shortly after went on a job hunt for which I was trying to upskill. Then I got a job and needed to reskill. All of which meant I was burnt out of learning new stuff. Ian, I’m sorry it took so long to produce this review. Hopefully, Rust isn’t old news now that I’ve finally got to writing a review! Luckily Paul Floyd wrote a review for CVu (that I mostly agree with) which has let me off the hook a bit.

I will start with a few gripes and then move on to more positive stuff. One of the difficulties of writing any instructional material is deciding the skill level of your readers. The forward says this book is not for first time programmers, but I get the impression that the visualised reader has done some, possibly large, single file programming in something like Python but not much more. That in itself is not a bad thing, but it tries to make the reading experience personal by using ‘you’ a lot rather than a more indirect writing style. As such, it ends up having phrases like ‘Now that you know...’, ‘you now know that you can..’, ‘all you need to know...’ and ‘Given that you just learned about structs...’. I found this condescending and it annoyed me more than it perhaps should have (and did slow down my reading). It wouldn’t have taken much to avoid this with some re-wording (e.g. ‘now we have told you about…’). But I think an even bigger cause is that the structure of the book could be improved – at least for more experienced programmers.

Sometimes, I felt the descriptions floundered about a bit, trying multiple times to explain something (sometimes by simply rearranging the words!) and still ending up missing the mark. Other times, the language is a bit slack. For example, in the discussion on the match statement, it says, “If you want to run multiple lines of code in a match arm” – where in fact it means multiple statement and expressions. Still other times, it expends more verbiage on saying why what it’s about to tell you, or has just told you, is important than actually telling you the thing.

There is a hint of indoctrination into the cult of Rust – you can’t just program in Rust, you have to be Rustacean – but it is not too bad.

But if you can put those gripes aside and change the order you read the book in, I think it can be a useful book. I’ll review the content of the book in the order that I think you should tackle it in.

The first five chapters are in a sensible order. Chapter 1 explains installation, presents “Hello, World!” and introduces Cargo. Cargo is Rust’s Swiss army knife and does package management, project initialisation, building, running and testing! (Note, as an alternative to installing Rust locally you can use https://play.rust-lang.org/ or Compiler Explorer.) Chapter 2 is a simple program giving a brief tour of the language. Chapter 3 gives the basics of the language, including basic types, functions and loops. As an experienced programmer, you might feel the urge to skip this chapter, but Rust does have its own way of doing things, even at this level. For example, like Haskell, ifs are expressions that can yield a value and hence Rust does not have ternaries. Chapter 4 introduces ownership and the borrow checker; what Rust is famous for. Chapter 5 introduces struct and methods. This may sound like something that would be familiar to C++ programmers, but Rust structs are like C structs and the methods are in separate impl blocks.

At this point, I suggest deviating from the order of the book by going to chapter 7, ‘Managing Growing Projects with Packages, Crates, and Modules’. This completes the encapsulation of functionality into modules using the mod construct. Modules have public and private access, and this brings us to something equivalent to a C++ class.

The next jump is to the first part of chapter 10, ‘Generic Types, Traits, and Lifetimes’, as an introduction to generics. Traits are used in a number of ways in Rust, and in generics they act a bit like C++ Concepts. I think C++ has demonstrated that you can do a lot with generics without requiring traits, so leave the second part of chapter 10 on traits and lifetimes to later and just focus on the basic generics.

Now we can come back to chapter 6, ‘Enums and Pattern Matching’. Rust enums are really nothing like C++ enums. They are more like Haskell data types or a built-in std::variant. After the borrow checker, I would say these are the most exciting part of Rust and have quite an impact on how you write Rust code. It includes Option<T> which is like Haskell’s Maybe and C++’s std::optional.

Since we’re now in the mindset of Patterns and Matching, it’s worth visiting chapter 18, which is a nice clinical, almost fun, list of all the ways Rust does Patterns and Matching.

After that, return to the nitty-gritty of error handling in chapter 9, mainly using Optional<T>, Result<T, E> (like std::expected) and panic!

Step back one to chapter 8, ‘Common Collections’, then forward 5 to chapter 13, ‘Functional Language Features: Iterators and Closures’.

We’ve put off traits as long as we can, so it is off to the second part of chapter 10 for ‘[Advanced] Generic Types, Traits, and Lifetimes’. Follow this with chapter 15, ‘Smart Pointers’. These two chapters are perhaps the two most demanding chapters in the book. After them, chapter 16, ‘Fearless Concurrency’, and chapter 17, ‘Object Oriented Programming Features of Rust’, feel like light relief!

Chapter 19, ‘Advanced Features’, covers Rust’s macros (among other things). There are different types of Rust macro. They are not your C’s macros!

Now the Rust language has been mostly dealt with, it’s time to give Cargo some attention. As mentioned, this is Rust’s Swiss army knife and is deserving of its own section. Visit chapter 11, ‘Writing Automated Tests’, and chapter 14, ‘More about Cargo and Crates.io’.

I personally didn’t get much out of the projects in the book. They seemed to just slow down learning the language and got bogged down in the problem domain rather than explaining the language. So I’ve put chapter 12, ‘An I/O Project: Building a Command Line Program’, and chapter 20, ‘Final Project: Building a Multithreaded Web Server’, at the end, to be used as ‘end of course assessment and review’.

While I’m glad to have finally got my review done, I don’t think this will be the last time I look at this book. I don’t know what other books there are on Rust, but I do feel this book does cover all the bases, and certainly has enough to get you Googling for more details about Rust.

Website: https://nostarch.com/rust-programming-language-2nd-edition






Your Privacy

By clicking "Accept Non-Essential Cookies" you agree ACCU can store non-essential cookies on your device and disclose information in accordance with our Privacy Policy and Cookie Policy.

Current Setting: Non-Essential Cookies REJECTED


By clicking "Include Third Party Content" you agree ACCU can forward your IP address to third-party sites (such as YouTube) to enhance the information presented on this site, and that third-party sites may store cookies on your device.

Current Setting: Third Party Content EXCLUDED



Settings can be changed at any time from the Cookie Policy page.