Hello, dev.to community! This is my first post, so please be kind. This will be about my experience and impressions today as a C++ developer learning the Rust programming language.
I've been streaming on Twitch for a couple years now. Most of the time, I'm doing a combination of work on an indie game dev project and showing the process of software development to others, all while interacting with viewers and trying to answer their questions. That includes working through problems, building up designs, and often learning new things myself.
Today I had a somewhat different stream. Over the course of the last few months, many viewers have either recommended or asked about Rust, a relatively new programming language I had only heard rumors about. So I decided I would learn Rust. Live, on stream. I had a blast!
I began the day with a vague impression of what Rust is, and a handful of links. Chris Griffing, who I look up to as an expert web-dev guru, gave me a lot of encouragement, as well as some links to start with, including a link to the Rust Book.
In four hours, I made it through the first three chapters of the book. So far, I like the book, and the language. I could have probably gone a bit faster, but I had a lot of people looking at it with me, and they had many questions, ideas, and suggestions.
I've been using C++ for a long time. Learning a new programming language, once you already know a few of them, is mostly about recognizing what's similar and what's different between what you already know and what you're learning. So I thought it might be interesting and perhaps useful for others if I took notes as I learned Rust, especially pointing out the things that are distinctly different between C++ and Rust.
So far, these are my observations:
- Although Rust is a compiled language and so feels like C++, it has a nice packaging system (
cargo) that reminds me strongly of Node's
- Variables are immutable by default but adding
mutmakes them mutable. This is exactly opposite of C++, where you need
constto make something immutable. (Apparently,
constis the evil twin of
- The type binding syntax is different, but has the same concept in C++. Without any type annotation, it's like using
autoin C++. Adding a type annotation is like using an explicit type in C++, except the type annotation comes after the variable name, not before (and there's also a colon to deal with).
- Creating a new object involves calling a special function "new" on the type (example:
- It lacks parenthesis around its argument (which is true of Rust expressions in general).
- Instead of the
casekeyword, we use arrows between the case and the code for the case (Rust calls the code an "arm").
- It is a first-class expression; in other words, it returns a value, similar to a function; each arm should "return" something, as in evaluate to an expression whose value will be returned if that arm is chosen.
cmpis a method you can use on anything that can be compared, and it returns an enumerated type
- We can shadow variables in the same scope (you can do this in C++ but you have to create a inner scope to do it).
- A common pattern is to use shadowing to "update" an immutable variable.
- Integer overflow is checked and a panic is generated on overflow, but only in debug builds.
- The "char" type is actually 4 bytes and contains the character's Unicode code point, rather than being simply ASCII or implying some kind of encoding like UTF-8.
- The "tup" (tuple) type can be destructured similar to array/object destructuring in ES6.
- You can also index a tuple by literal integer index (i.e. "x.0").
- Array type annotations are a bit alien:
- [i32; 5] means array of five 32-bit integers.
- Alternate array initialization syntax which looks similar to array type annotation:
- [3; 5] means initialize to be a five-element array, where each element is initialized to have the value 3.
- Blocks are also expressions! Sweet!!
- Function return type specifications are like they are in C++ if you use the
autokeyword (only in Rust you don't need the
- Rust is a bit (too much, in my opinion) opinionated about a few things:
- Functions and variables should use snake_case.
- The last line of a function should not be a statement, but just an expression that becomes the return value implicitly.
- Rust's idea of "void" is an empty tuple () <-- neat
- The "for" loop is like C++'s for-colon syntax, only using "in" instead of the colon (similar to Python?)
- Standard library defines a
Rangetype which lets you form sequence generators like this: (0..4)
ifare expressions too!
That was it for my first day of learning. I'm really looking forward to the second day! You can bet I will live stream the experience too.