Creative Projects for Rust Programmers
eBook - ePub

Creative Projects for Rust Programmers

Build exciting projects on domains such as web apps, WebAssembly, games, and parsing

Carlo Milanesi

Share book
  1. 404 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Creative Projects for Rust Programmers

Build exciting projects on domains such as web apps, WebAssembly, games, and parsing

Carlo Milanesi

Book details
Book preview
Table of contents
Citations

About This Book

A practical guide to understanding the latest features of the Rust programming language, useful libraries, and frameworks that will help you design and develop interesting projects

Key Features

  • Work through projects that will help you build high-performance applications with Rust
  • Delve into concepts such as error handling, memory management, concurrency, generics, and macros with Rust
  • Improve business productivity by choosing the right libraries and frameworks for your applications

Book Description

Rust is a community-built language that solves pain points present in many other languages, thus improving performance and safety. In this book, you will explore the latest features of Rust by building robust applications across different domains and platforms.

The book gets you up and running with high-quality open source libraries and frameworks available in the Rust ecosystem that can help you to develop efficient applications with Rust. You'll learn how to build projects in domains such as data access, RESTful web services, web applications, 2D games for web and desktop, interpreters and compilers, emulators, and Linux Kernel modules. For each of these application types, you'll use frameworks such as Actix, Tera, Yew, Quicksilver, ggez, and nom. This book will not only help you to build on your knowledge of Rust but also help you to choose an appropriate framework for building your project.

By the end of this Rust book, you will have learned how to build fast and safe applications with Rust and have the real-world experience you need to advance in your career.

What you will learn

  • Access TOML, JSON, and XML files and SQLite, PostgreSQL, and Redis databases
  • Develop a RESTful web service using JSON payloads
  • Create a web application using HTML templates and JavaScript and a frontend web application or web game using WebAssembly
  • Build desktop 2D games
  • Develop an interpreter and a compiler for a programming language
  • Create a machine language emulator
  • Extend the Linux Kernel with loadable modules

Who this book is for

This Rust programming book is for developers who want to get hands-on experience with implementing their knowledge of Rust programming, and are looking for expert advice on which libraries and frameworks they can adopt to develop software that typically uses the Rust language.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Creative Projects for Rust Programmers an online PDF/ePUB?
Yes, you can access Creative Projects for Rust Programmers by Carlo Milanesi in PDF and/or ePUB format, as well as other popular books in Ciencia de la computación & Lenguajes de programación. We have over one million books available in our catalogue for you to explore.

Information

Year
2020
ISBN
9781789343878
Using a Parser Combinator for Interpreting and Compiling
Rust is a system programming language. A typical task of system programming is processing formal languages. Formal languages are languages specified by well-defined logical rules and used everywhere in computer technology. They can be broadly classified into command, programming, and markup languages.
To process formal languages, the first step is to parse. Parsing means analyzing the grammatical structure of a piece of code to check whether it respects the rules of the grammar it is supposed to use, and then, if the grammar is respected, to generate a data structure that describes the structure of the parsed piece of code, in a way that such code can be further processed.
In this chapter, we will see how to process text written in a formal language, starting from the parsing step and proceeding with several possible outcomes—simply checking the grammar, interpreting a program, and translating a program into the Rust language.
To show such features, an extremely simple programming language will be defined, and four tools (syntax checker, semantic checker, interpreter, and translator) will be built around it.
In this chapter, you will learn about the following topics:
  • Defining a programming language using a formal grammar
  • Classifying programming languages into three categories
  • Learning two popular techniques for building parsers—compiler-compilers and parser combinators
  • Using a parser combinator library for Rust named Nom
  • Processing a source code to check its syntax following a context-free grammar, using the Nom library (calc_parser)
  • Verifying the consistency of variable declarations and their usage in some source code, and at the same time preparing the required structure for optimal execution of the code (calc_analyzer)
  • Executing the preprocessed code, in a process named interpretation (calc_interpreter)
  • Translating the preprocessed code into another programming language, in a process named compilation (calc_compiler); as an example, translation to Rust code will be shown
After reading this chapter, you will be able to write the grammar for a simple formal language or understand the grammar for an existing formal language. You will also be able to write an interpreter for any programming language by following its grammar. Also, you will be able to write a translator for a formal language into another formal language, following their grammar.

Technical requirements

To read this chapter, knowledge of the preceding chapters is not required. Some knowledge of formal language theory and techniques is useful but not required, because the required knowledge will be explained in this chapter. The Nom library will be used to build such tools, and so it will be described in this chapter.
The complete source code for this chapter is in the Chapter08 folder of the GitHub repository, located at https://github.com/PacktPublishing/Creative-Projects-for-Rust-Programmers.

Project overview

In this chapter, we will build four projects of increasing complexity, listed as follows:
  • The first project (calc_parser) will just be a syntax checker for the Calc language. Actually, it is just a parser, followed by a formatted debugging print of the parsing result.
  • The second project (calc_analyzer) uses the parsing result of the first project to add the verification of the consistency of the variable declarations and of their usage, followed by a formatted debugging print of the analysis result.
  • The third project (calc_interpreter) uses the analysis result to execute the preprocessed code, in an interactive interpreter.
  • The fourth project (calc_compiler) uses the analysis result again to translate the preprocessed code into equivalent Rust code.

Introducing Calc

To make the following explanations, we will first define a toy programming language that we will name Calc (from the calculator). A toy programming language is a programming language used to demonstrate or prove something, not designed to develop real-world software. A simple program written in Calc is shown as follows:
@first
@second
> first
> second
@sum
sum := first + second
< sum
< first * second
The preceding program asks the user to type two numbers and then prints the sum and the product of those numbers on the console. Let's examine one statement at a time, as follows:
  • The first two statements (@first and @second) declare two variables. Any variable in Calc represents a 64-bit floating-point number.
  • The third and fourth statements (> first and > second) are input statements. Each of these prints a question mark and waits for the user to type a number and press Enter. Such a number, if valid, is stored in the specified variable. If no number or an invalid number is typed before pressing Enter, the value 0 is assigned to the variable.
  • The fifth statement declares the sum variable.
  • The sixth statement (sum := first + second) is a Pascal-style assignment. It computes the sum of the first and second variables and assigns the result to the sum variable.
  • The seventh and eight statements perform output. The seventh statement (< sum) prints on the console the current value of the sum variable. The eighth statement (< first * second) computes the multiplication between the current values of the first and second variables, and then prints on the console the result of such multiplication.
The Calc language has two other operators—- (minus) and / (divide)to specify subtraction and division, respectively. In addition, the following code shows that the operations can be combined in expressions, and so these are valid assignment statements:
y := m * x + q
a := a + b - c / d
Operations are performed left to right, but multiplication and division have higher precedence than addition and subtraction.
In addition to variables, numeric literals are also allowed. So, you can write the following code:
a := 2.1 + 4 * 5
This statement assigns 22.1 to a, as multiplication is performed before addition. To force different precedence, parentheses are allowed, as illustrated in the following code snippet:
a := (2.1 + 4) * 5
The preceding code snippet assigns 30.5 to a.
In the preceding code snippet, there are no characters that separate a statement from the next one, in addition to the newline characters. Actually, the Calc language has no symbols used to separate statements, and also, it does not need them. So, the first program should be equivalent to this:
@first@second>first>second@sum sum:=first+second<sum<first*second
In the preceding code snippet, there is no ambiguity because the @ character marks the start of a declaration, the > character marks the start of an input operation, the < character marks the start of an output operation, and a variable in a location where the current statement does not allow a variable marks the start of an assignment.
To understand this syntax, some grammatical terms must be explained, as follows:
  • The whole text is a program.
  • Any program is a sequence of statements. In the first example program, there is exactly one statement for each line.
  • In some statements, there can be an arithmetic formula that can be evaluated, such as a * 3 + 2. This formula is an expression.
  • Any expression can contain sums or subtractions of simpler expressions. The simpler expressions that contain neither sums nor subtractions are named terms. Therefore, any expression can be a term (if it contains neither sums nor subtractions), or it can be the sum of an expression and a term, or it can be the subtraction of an expression and a term.
  • Any term can contain multiplications or divisions of simpler expressions. The simpler expressions that contain neither multiplications nor divisions are named factors. Therefore, any term can be a factor (if it contains neither multiplications nor divisions), or it can be the multiplication of a term and a factor, or it can be the division of a term and a factor. There are three possible kinds of factors, l...

Table of contents