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!
First Day of Learning
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.
Things about Rust that Stand Out From a C++ Perspective
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'snpm
tool. - Variables are immutable by default but adding
mut
makes them mutable. This is exactly opposite of C++, where you needconst
to make something immutable. (Apparently,const
is the evil twin ofmut
). - The type binding syntax is different, but has the same concept in C++. Without any type annotation, it's like using
auto
in 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:
String::new()
rather thannew String
orString()
. -
match
is likeswitch
but:- It lacks parenthesis around its argument (which is true of Rust expressions in general).
- Instead of the
case
keyword, 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.
-
cmp
is a method you can use on anything that can be compared, and it returns an enumerated typeOrdering
. - 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
auto
keyword (only in Rust you don't need theauto
obviously). - 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
Range
type which lets you form sequence generators like this: (0..4) -
match
andif
are 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.
Top comments (8)
Hi
Actually, it is NOT (it's a strict subset).
Unicode is tricky, I know.
I must have misunderstood the book; thanks for pointing this out! I'll have to go back and study that part in more detail.
That's funny, I'm a long time blogger and I'd like to start streaming on twitch too. Do you have any pointers for a beginner? I'll trade any advice you have for twitch with a blogging tip - Deal?
I can't sum it up any better than how @adamlearns does in his blog post.
Live-streaming yourself developing
Adam ・ Jun 1 ・ 15 min read
Nice - this answers all my questions - and more. Thanks a lot. I owe you one :)
Great, nice article!
About the opinionated naming, it does make sure that all Rust code looks and feels the same. This is especially true when rustfmt and cargo clippy are used.
I do feel that such a thing ought to be handled by clippy and rustfmt, not by the compiler itself. It's not the compiler's job to enforce formatting standards IMO.
Only naming is done by the compiler.
I do get where you're coming from, though. But if it's not enabled by default, people just won't use it. That's sadly the case.
You can opt out of you want, but you'll need to do that in every project.