RUST
Rust: How to start coding your first apps
David Bolton introduces Rust programming by setting it up then running and debugging a program to analyse text.
Part One! Don’t miss next issue, subscribe on page 16!
OUR EXPERT
David Bolton brings decades of experience, including how to run-play-bymail games.
QUICK TIP
You can install Visual Studio Code from https://code. visualstudio. com/Download by downloading the relevant file, or on Ubuntu, you can use Snap or $ sudo apt install file.deb where file is the offered version, currently code_1.96.21734607745_ amd64. Then run code from the terminal. Our preference is the non-Snap version because it’s usually more up to date.
This is the first in a new series of tutorials about learning Rust. Instead of writing about the T language, this will have a more practical bent, with useful programs and then explaining the features of Rust that they use.
The first program is one that reads a text file and calculates the Gunning fog index (https://bit.ly/ lxf325index). This is a measure of how readable text is. It’s not the most complicated task but it has one or two interesting points. Let’s divide the task into a series of smaller subtasks: 1. Read text in from a file. 2. Process the text so we can identify how many sentences there are and the average length of each sentence. Then calculate the average sentence length: the number of words/the number of sentences. 3. Count the number of complex words, where a complex word is defined as having three or more syllables. This has to exclude proper nouns, familiar jargon or compound words. 4. Add the average sentence length and the percentage of complex words.
Getting started with Rust
First work your way through the boxout on installing Rust (page 96). Our new app will be called Fog. We put all our Rust apps in a top folder called rust but you can put them anywhere writable. As cargo new creates folders, our starting point is the home/rust folder.
First close the VS folder in VS Code, then close VS Code. When you use cargo, it’s easiest if you run it from outside VS Code, but you can run it inside a VS Code terminal. The cargo new command creates a folder for the project, so let’s do that with this: $ cargo new fog $ cd fog $ code .
Opening VS Code with the full stop (period) makes the current folder your work folder in VS Code.