You are a JavaScript or Node.js developer, and you’re interested in Rust programming. But I need to figure out where to start. How can you translate your JavaScript and Node.js knowledge into Rust language? This article will show you the main differences between JavaScript & Rust. I also explain how they relate so you can better understand what to expect once you begin using Rust.
Type system
Let’s start with the most significant difference: the type system. Hearing about a type of system can be overwhelming if you are from something other than JavaScript. Rust’s plan, which I’ve worked with for years, is among the most intuitive and expressive.
Rust is a strong and static language. JavaScript is dynamic and weakly typed. Let’s see what this means for your daily code.
Dynamic vs. static typing
Static typing means that all variables must be known at compile, not runtime. Statically-typed languages don’t depend on user input and can therefore change at the runtime or any other information not known at compile.
JavaScript uses dynamic typing, while Rust uses static type. Let’s see how this translates into a bit of code.
Rust has more advanced type inferencing capabilities than other statically typed languages like Java or C++. This is an interesting fact to notice about the Rust code. You only sometimes need to specify the type in Rust code, except in your function definitions and structs. This is a balanced approach that I like. It’s simple to read for anyone who looks at a function signature, and the person implementing it doesn’t have to worry about entering everything.
Rust has a way of dealing with variables whose types you want to change during the execution of functions. Here’s an example: If your part contains a number but needs it to be used in another operation, this is how you can do it.
This pattern is called shadowing by Rust. This pattern is pervasive in Rust codebases. Rust creates a new variable each time you use the allow keyword. It doesn’t care whether the assignment is of a different type than the one before it. It’s just another variable, as any other for Rust.
Let’s look closer at Rust’s dynamic typing and JavaScript’s static typing.
Strong vs. weak typing
Although static and dynamic typing are easily compared (types can be known at compile-time or not), strong and weak typing are more complicated as these types look more like a scale on which languages can fall. These terms measure how many languages allow implicit conversions from one type to another.
A more typed language allows for fewer implicit conversions than one with many. Rust’s VERY ranking is based on this definition. There is one example of implicit conversion, and it can only be accessed in particular circumstances.
JavaScript is on the other side of the weak scale because implicit conversions exist everywhere. Every variable you assign has an implicit conversion element, regardless of whether you know it. This is because every object you declare has additional properties provided by JavaScript.
How does this affect your code?
Let’s compare two similar pieces of JavaScript and Rust code.
Rust (Strong)
JavaScript implicitly converted a number into a string, as you can see from the code above. You must explicitly mention that Rust transforms ten into a line before adding a string slice.
Although everyone may have different preferences, I prefer that my code is clear, especially when working with larger code bases, where you don’t have to write all the code. This clarity lets you understand the original author’s intent and avoids mistakes such as wondering why your number is 100 rather than ten after adding 0.
After we have looked at the differences between the types of systems, let’s look into mutability, which is an important topic.
Mutability
You already know three ways to declare a variable in JavaScript modern.
let
Const
var
Each works differently, but I will only refer to let and cont since both let and var are identical in mutability.
There are three ways you can declare a Rust variable
let
let out
const
They each have different mutability functions. It’s easy to see that let declares a mutable variable. But if this is true, what’s different between let or const? Why do you need to use two words when expressing a variable that should be mutable in Rust?
It would help if you answered the first question by remembering that Rust is a compiled language, not JavaScript. This is the crucial difference between let & cont. They are both immutable, but the difference is when the variable is initialized. (Note: I use initialized here rather than declared).
A const must be initialized at compile-time. Every occurrence of the variable must also be inlined at its referenced site. This is evident in the PI value. You would want to keep this value constant throughout your application. On the other hand, a let variable allows you to initialize it as a variable that is only known at runtime, such as user input. Although this answers the first question, you might still be curious why creating a mutable variable takes so much more.
Rust is heavily influenced by functional programming. In purely available programming, all values are immutable. This can significantly increase CPU load and memory usage, but there are many benefits. I won’t be able to go through them all, but one of the most important is when you try to reason about parallel/concurrent code. Rust doesn’t follow pure functional programming. You can still declare mutable variables for performance reasons. However, they want you to consider whether that variable is necessary.