Category Archives: rust

Polymorphism in Rust: Enum vs Trait + Struct

Rust isn’t an object-oriented language in the same way as C++ and Java. You cannot write classes that extend behavior from another class for example. However, Rust does support polymorphism and there are two common approaches. For this blog post I’m going to use an example of writing a crate for generating svg (scalable vector graphics) files based on a… Read more »

Implementing a Pratt parser in Rust

Following on from the previous tutorial that demonstrated how to¬†implement¬†a tokenizer, it is now time to implement a parser to transform the list of tokens into an Abstract Syntax Tree, hereafter referred to as an AST. We will be using a Pratt parser design, which is a recursive descent parser that is very simple to reason with. I discovered this… Read more »

Writing a Tokenizer with Iterator and Peekable

In this tutorial we will write a low-level tokenizer for tokenizing simple math expressions like “12+34*56” using Rust’s Iterator and Peekable traits. First, we need to declare some data structures for storing the results. Rust only has two types of data structure – struct and enum. If you’re coming from a Java or Scala background you would possibly be wanting… Read more »