Learning Rust by Contrasting with TypeScript: Part 14

John Tucker
codeburst
Published in
3 min readDec 1, 2019

--

Wrapping up series (yeah!) with a variety of topics (trait objects and match patterns).

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

The code for this section is available to download in two parts; Rust download and download and TypeScript download and download.

Using Trait Objects That Allow for Values of Different Types

Let us walk through an example in the Rust Book Object Oriented Programming Features of Rust section and contrast them with TypeScript.

In order to allow values of different types that are bound by a trait, we need to introduce a new concept: trait objects:

A trait object points to both an instance of a type implementing our specified trait as well as a table used to look up trait methods on that type at runtime.

— Rust — Using Trait Objects That Allow for Values of Different Types

The key to understanding the need for trait objects is that Rust needs the pair (type of struct, trait) to determine the implementation. At compile time, however, the compiler does not know all the possible types of structs implementing the trait; thus the need for the look up table.

This is reminiscent of the need for trait bounds when using traits as parameters; the parameter needs to hold the pair (type of struct, trait):

We can contrast this to TypeScript, where the object itself holds the implementation; so we only need to accept an interface as the parameter.

In the following example, we see that we have to use the trait object (dyn Draw) as the type supplied to Box:

note: It is important to note that we needed to use the trait object with a pointer (Box); apparently this is explained in an advanced topic.

In TypeScript, we have a much simpler structure consisting of an array typed based on the interface only:

Patterns and Matching

Let us walk through an example in the Rust Book Patterns and Matching section and contrast them with TypeScript.

The main gist of this section is to illustrate that much, even the simple let command, of Rust revolves around pattern matching.

Looking at the TypeScript equivalent, we see that the syntax is similar to the Rust examples. In the case of TypeScript, however, we use the term destructuring to describe much of this same feature.

Observations:

  • One important difference is that in Rust, the underscore has special meaning (essentially indicating to ignore part of the pattern), in TypeScript it is just a variable that is happened to be named underscore by convention

Wrap Up

Ok, this series went on for a lot longer than I expected. I am sure there is more to learn, but thinking it is time to learn in the context of a real project. Bought the following book (arriving today), and am hoping it gets me started on the right path.

Programming WebAssembly with Rust: Unified Development for Web, Mobile, and Embedded Applications

--

--