Rust gained some attention over the past few weeks so I decided to jump in and read Rust By Example.
Rust By Example
The book is well designed and easy to read on desktop and mobile. The example code can be modified by the user and compiled/run in the webpage so if you have any queries about how a particular language feature works you can find out right there.
There were a couple of things about it that I found confusing:
- Methods are introduced in chapter 25, after having been used a few times in earlier chapters.
- ADT enums are introduced before normal (C-style) enums.
- While I was going through the book the language changed (!), some of the examples wouldn’t run - for example I had to use
Box::new
instead ofbox
.
Initial Impressions
- Nice string formatting features, with compile time safety.
- Immutable variables by default, pretty unusual.
- I like scoping variables with {}.
- Expressions: You omit the semicolon at the end of a block to return the value of its last expression…This feels strange but is useful (expression page here).
- Named looped, very nice.
- Pattern matching: Great. I liked these in Haskell.
- Generics: Good.
- Automatic Memory safety/borrow checker: The compiler will give you errors if you use memory incorrectly (for example if you try to deference a pointer when the memory has been deallocated). This seems to be one of the main selling points of Rust.
- Lifetimes: I’ve not looked deeply into this. Seems like the lifetime of an object becomes part of its type(?)
- Higher order functions: Pretty much expect this from any modern language. It comes out of the box with functional programming functions like map.
I’d say Rust probably has a good future. There are benefits over C++ from a language perspective. There is current lack of libraries and tools and the language does have some instability. Maybe it can eventually be language of choice for game development and other domains that need high performance programs.