Book Image

Learning Rust

By : Vesa Kaihlavirta
Book Image

Learning Rust

By: Vesa Kaihlavirta

Overview of this book

Rust is a highly concurrent and high performance language that focuses on safety and speed, memory management, and writing clean code. It also guarantees thread safety, and its aim is to improve the performance of existing applications. Its potential is shown by the fact that it has been backed by Mozilla to solve the critical problem of concurrency. Learning Rust will teach you to build concurrent, fast, and robust applications. From learning the basic syntax to writing complex functions, this book will is your one stop guide to get up to speed with the fundamentals of Rust programming. We will cover the essentials of the language, including variables, procedures, output, compiling, installing, and memory handling. You will learn how to write object-oriented code, work with generics, conduct pattern matching, and build macros. You will get to know how to communicate with users and other services, as well as getting to grips with generics, scoping, and more advanced conditions. You will also discover how to extend the compilation unit in Rust. By the end of this book, you will be able to create a complex application in Rust to move forward with.
Table of Contents (21 chapters)
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Title Page
Preface
Free Chapter
1
Introducing and Installing Rust
4
Conditions, Recursion, and Loops

Installing Rust


As with most languages, Rust is available for a wide number of platforms. It would be impossible to go through installing the compiler on every variant of every operating system. Fortunately, there's an official method of installing Rust, and even though the details may differ slightly, the process is almost the same on all platforms. Therefore, this book will cover installing Rust using rustup on Fedora 27.

https://rustup.rs always contains up-to-date instructions on how to get going on all platforms. On Linux and macOS, it will look something like this:

On Windows, this text is replaced by a link to rustup-init.exe, which is an executable that installs and sets up rustup on Windows.

Installing rustup on Linux

Run the suggested command that is shown at https://rustup.rs. Run this command in a Terminal. The script suggests some defaults and asks you to confirm them. This is roughly what it should look like after completing the whole script:

Note that this script attempts to set up rustup for your user by editing your .profile and .bash_profile files. If you are using a custom setup, such as another shell, you may need to add the source $HOME/.cargo/env command manually.

After finishing this script, you can verify that it worked by logging off and on from your Terminal and verifying that the tools are in your path:

gcc prerequisites

To build any software that links against external libraries, you will need a C compiler and development versions of any libraries you may be linking against. To ensure that things work properly, install the compiler using the standard method for your operating system.

In Fedora, this would be done using the dnf tool:

sudo dnf install -y gcc

If you are unsure whether you have gcc installed, type the following command in a terminal window:

gcc -version

If gcc is installed, you'll see something like this:

Testing your installation

Open a command-prompt window and type this:

rustc --version

If everything was installed correctly, you will see something like this:

Integrated Development Environment

To effectively code Rust, you will need at least some sort of text editor. All popular editors are properly supported, so if your favorite is Vim, Emacs, or any of the others, you will find a high-quality Rust extension there. The website https://areweideyet.com/ should give a current view of how things are.

We will cover the lightweight IDE from Microsoft, Visual Studio Code, and its most current Rust extension, called simply Rust. This IDE should work fairly well in all the different desktop environments. Installation instructions and packages for several platforms are available at Visual Studio Code's main site, https://code.visualstudio.com.

  1. Open up Visual Studio Code and go to the Command Palette, either by the View menu or by the keyboard shortcut Ctrl + Shift + P (which may differ between platforms). Type in install extension to look for the proper command, and then select Install Extensions:
  1. After selecting this, type rust into the next field to look for the Rust extension. At the time of writing, the most recent one is made by kalitaalexey:
  1. You can install Rust right away by pressing Install; alternatively, click on the list item itself to show information about the extension first. After installing it, reload the editor. The Rust extension is now installed and ready to use!