DEV Community

Cover image for From JavaScript to Rust : Data Types

Posted on • Originally published at

From JavaScript to Rust : Data Types

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.


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.


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.


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.


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.


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.


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.


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.

Top comments (6)

nicozerpa profile image
Nico Zerpa (he/him)

Great article, Laurie! It reminded me of when I learned C++ after learning JavaScript. That was many many many many many years ago! In the beginning, I was confused with things like arrays the different types of numbers.

But once you get used to it, it makes it easier to understand other strictly typed languages.

stojakovic99 profile image
Nikola Stojaković

C++ was my first language and it helped immensely in learning all other languages, even though I don't recommend it to the beginners today.

laurieontech profile image

Ya, my original background is Java and Python so the C similarities are new!

maks_yadvinskyy profile image
Maksym Yadvinskyy

Thank you for this article. I’ve tried so many times to enter Rust Lang and just end up being discouraged because there is this overhead of learning the syntax and if you want to use any of the cargo packages there is a learning curve of understanding how the author wrote the package. Maybe I should keep trying it should click eventually.

ssimontis profile image
Scott Simontis

Trying to learn Rust was probably the most difficult thing I have ever done. I have a background in embedded C, so Rust is incredibly appealing, but it is such a mental shift that I probably need to take another go at it. Thanks for making the Rust community more accessible!

ben profile image
Ben Halpern

This is great stuff