Learning Rust by Contrasting with TypeScript: Part 2
By: John Tucker
Tags: Programming, Typescript, Web Development, Rust

We explore variables, mutability, and data types.

This article is part of a series starting with Learning Rust by Contrasting with TypeScript: Part 1.

The code for this article is available to download; Rust download and TypeScript download.

Addendum 11/5/19: Ended up adding the Control Flow section with a separate Rust download and TypeScript download.

Variables and Mutability

Let us walk through the examples in the Rust Book Variables and Mutability section and contrast them with TypeScript.

With Rust, a variable set with let as shown is immutable.

In TypeScript, the equivalent is a variable set with const.

note: In both Rust and TypeScript the variable types are inferred from the right-hand-side; thus explicit typing is not required.

With Rust, a variable set with let mut as shown is mutable.

In TypeScript, the equivalent is a variable set with let.

With Rust, a variable set with let and let mut can be set at run-time:

Likewise for TypeScript for variables set with both const and let:

Rust has another immutable variable type; const. Unlike, let however, const cannot be set at run-time (only set at compile-time). The upper-case const variable names is by convention.

TypeScript does not have an equivalent variable type. For this situation, one typically uses the TypeScript const variable type and simply name it with upper-case by convention.

Rust allows one to redefine, or shadow, a variable of type let:

note: It is important to observe that the resultant variable a can still be immutable. Also, while not illustrated in this example, the variable types can also differ through shadowing.

TypeScript does not have an equivalent feature, i.e., TypeScript cannot redeclare a block-scoped variable. In TypeScript, one often simply creates a new const variable name in this situation.

Data Types

Now we walk through the examples in the Rust Book Data Types section; starting first with the Rust scalar types.

Let us consider the equivalents (called primitives) in TypeScript:

One big difference is that in JavaScript (and thus TypeScript) all numbers are 64-bit floating point where-as Rust there are a number of integer and floating point types.

Another difference is that JavaScript (and thus TypeScript) has a primitive string type that can contain an arbitrary number of characters; not just a single character.

Rust has two compound types; tuple and array. A tuple being a fixed length ordered list of values of varying types. An array being a fixed length ordered list of values of the same type.

In TypeScript, a tuple is essentially the same as in Rust. An array, however is of arbitrary length, e.g., we can append a value.

Sidebar into Object or Reference

So far, Rust and TypeScript have been relatively similar; however there is a big difference lurking in compound types. The difference is in how each of them store compound types in variables.

In Rust, the value of a compound type variable is the object itself.

Observations:

We have to use let mut for tup2 in order to mutate the value later

Assigning tup2 to tup3 (copying their value) creates a completely new tuple

The result is that mutating tup2 has no relevance to tup3

Some logic applies to arrays

On the other hand, in TypeScript, the value of a compound type is a reference to the object.

note: In TypeScript, primitive types (boolean, number, string) behave like Rust with the value of the variable being the object itself.

Observations:

We can use const for tup2 because we are mutating the object and not the reference

Assigning tup2 to tup3 (copying their value) copies the reference to the same tuple object

The result that mutating tup2 is really mutating the common tuple object referenced by both tup2 and tup3

Same logic applies to arrays

I can already see that this is going to take me some getting used to as I have the TypeScript pattern etched into my brain.

Addendum 11/8/19: Having written a couple of more articles, I have come to realize (believe it was buried in the Rust book too), that the compound types tuple and array are likely only to be used as constants, e.g., days of the week, and as such this difference between Rust and TypeScript is irrelevant. Specifically, we will likely be using the Rust Vec type as the equivalent to TypeScript arrays.

Control Flow

The core concepts around flow of control are virtually identical between Rust and TypeScript; confirming this by walking through examples in the Rust Book Control Flow section.

The same implemented in TypeScript:

Observations:

The Rust pattern of having the last line of a function being an expression (with no semi-colon) to mean the return value was novel to me

Having gotten used to the TypeScript ternary operator, found the Rust equivalent a bit verbose

With Rust, having a loop return a value is novel to me

Next Steps

We dive into the complexities of ownership in the next article: Learning Rust by Contrasting with TypeScript: Part 3.