From JavaScript to Rust : Data Types

Laurie - Apr 27 '21 - - Dev Community

You may have been following my journey to learn Rust as I covered topics like mutable variables, ownership and references and crates. These learnings fell into two distinct categories.

  • Stuff that has a clear parallel in JavaScript land
  • Stuff that's brand new, and I have no existing mental model for

Yet, as I started to read and write more Rust code, I realized I was missing one big piece of the puzzle when it comes to learning a new language. I didn't have a strong understanding of data types in Rust. I quickly discovered that this information fell into a third "learning category". Stuff that overlaps with my JavaScript mental model in a way that's contradictory.

Given that reality, I figured a post was in order! So without further ado, let's talk data types.

Primitives

A JavaScript primitive is similar to what Rust calls a scalar type. Rust has four scalar types: booleans, integers, floating point numbers, and characters.

Additionally, The Rust Reference mentions primitive types, which includes str and Never on top of the four scalar types.

Boolean

Let's start with something familiar, booleans. Booleans, or bool in Rust, are a type with two possible values: true or false. This matches up with our JavaScript understanding, so no changes here.

Number

The same can't be said of numbers. In Rust, you need to care about what type of number you're storing. Is it an integer or a floating point number? What amount of space, in bits, does storing it take? Is it signed or unsigned?

In Rust you can initialize integers of five different sizes, from 8-bit to 128-bit, either signed or unsigned. For floating point numbers you have either f32 or f64; the first is single precision and the second, double precision.

String

You may have noticed that I didn't mention strings as Rust scalar types, I mentioned char. This is accurate. However, Rust does have a primitive type str. The strange thing is you likely won't use it other than as a reference to a specific string stored in memory, &str.

If you want to create or manipulate strings, you want the growable type String. But be careful! The methods you're used to, like len (aka length), won't return what you expect.

Structural types

In JavaScript, most structural types are specific implementations of the JavaScript object type. But we'll break them out individually for the sake of this post.

Arrays

Now we get to the part where you'll need to rewire your internal JavaScript dictionary. An array is a valid Rust type. Unfortunately, that's not entirely helpful. An array in Rust is of fixed size. If you expect to create static data that won't change but you want to reference/look it up in an array structure then this may be what you want.

More likely, though, you want a Vector. vec is similar to String. It's an array type that you can own and grow dynamically.

Objects

Search "Rust object type" and you're bound to be confused. You'll end up reading up on Trait object types, which is a bit more advanced than what you're after. What you're really looking for is struct.

In Rust, a struct is defined as a type composed of other types. Sounds like a JavaScript object to me! It's worth reading about structs further because they do some unique things when it comes to mutability.

Sets and Maps

These are relative newcomers in the land of JavaScript, but they're great tools to have at your disposal. When developing in Rust you'll get similar functionality from HashSets and HashMaps.

Note that there are also BTree based implementations of these structures.

Function

As with JavaScript, Rust has first-class functions. In addition to functions, defined using the fn keyword, Rust also has closures. Closures are anonymous functions with syntax that looks similar to JavaScript arrow functions.

Is that it?

JavaScript is pretty minimal when it comes to types. Especially when compared to more verbose languages like Java or C. Rust's offerings are more similar to the latter, so things like Linked Lists are available as a type in Rust.

If you can't find what you're looking for the docs are great! And so is the community. With a little bit of trial and error you'll get what you need.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .