Learning Rust for ex C++/Java developers (Part 1)

I’ve decided to start learning Rust.  It’s starting to be used across Mozilla and Mozilla has been heavily involved in defining it.

Additionally, I’ve been focused on JS  for a couple years now and this seems a nice language to get back to my C++/Java roots.  One of the main tutorials I’m reading is  here.

Rust is a strongly typed language and it’s main selling point is memory safety.  Rust will be a good language of choice for drivers or other systems programming.  Rust also has convenient ways to connect to other languages as a stack with many languages is quite commonplace these days.

Building and Running

Tools in Rust seem quite simple at first glance.  You can quickly build by running rustc.  Rust also has a sweet build and packaging system called cargo.  I found all this quite simple to get going with the basics.  I believe the “up and running” phase to be easier than Java and C++

First Impressions

Safety First!

I like how Rust makes safety a priority.  You’ll find TONS of safety checks built into Rust. For example, when you’re indexing into a vector you need to use a usize vs. an int; otherwise, you’ll get a compiler error which could save you from future grief.

Patterns

Rust has pretty powerful pattern matching capabilities.  Here’s one really cool thing:

In C++/Java a standard switch/case example:

switch (x) {
case 1:
// do something
break;
case 2:
// do the same thing as case 1
break;
default:
// do the default
break;
}

becomes the following in Rust:

match x {
case 1:
1|2 => // do something
_ => // do the default
}

Sweet!  A lot more brief in Rust.  One of the big selling points of Rust is the safety.  IF you forget to add a catchall ‘_’ you will get a compile time error.  Rust is demanding exhaustiveness here.

Traits

Initially, traits are a little difficult to grasp.  I started thinking of them as something similar to an interface in Java.  Interfaces are specified and then implemented inside of a class in Java.  In Rust, they are specified and then implemented inside of the struct.  Rust does have a trait called “drop.”  This is similar to destructor capabilities in C++.  It allows you to run some code when a variable goes out of scope.  You could use this to clean up resources that were used.  Additionally, it could be used to implement a reference count.

Testing

I took a cursory look at how unit tests can be written and it seems like a clean design.  There is an option to include the tests in the same file as the implementation or include a separate tests module.  The infrastructure seems quite simple and is integrated right in with cargo.  I particularly liked this.

Overall, I’m very enthusiastic about Rust and there is still lots to learn!

Advertisements

Author: tamarajhills

Tamara Hills is a Staff Software Engineer at Mozilla in the Connected Devices Group.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s