Reader small image

You're reading from  Practical System Programming for Rust Developers

Product typeBook
Published inDec 2020
PublisherPackt
ISBN-139781800560963
Edition1st Edition
Tools
Right arrow
Author (1)
Prabhu Eshwarla
Prabhu Eshwarla
author image
Prabhu Eshwarla

Prabhu Eshwarla has been shipping high-quality, business-critical software to large enterprises and running IT operations for over 25 years. He is also a passionate teacher of complex technologies. Prabhu has worked with Hewlett Packard and has deep experience in software engineering, engineering management, and IT operations. Prabhu is passionate about Rust and blockchain and specializes in distributed systems. He considers coding to be a creative craft, and an excellent tool to create new digital worlds (and experiences) sustained through rigorous software engineering.
Read more about Prabhu Eshwarla

Right arrow

Preface

The modern software stack is evolving rapidly in size and complexity. Technology domains such as the cloud, the web, data science, machine learning, DevOps, containers, IoT, embedded systems, distributed ledgers, virtual and augmented reality, and artificial intelligence continue to evolve and specialize. This has resulted in a severe shortage of system software developers able to build out the system infrastructure components. Modern societies, businesses, and governments increasingly rely heavily on digital technologies, which puts greater emphasis on developing safe, reliable, and efficient systems software and software infrastructure that modern web and mobile applications are built on.

System programming languages such as C/C++ have proved their mettle for decades in this domain, and provide a high degree of control and performance, but it is at the cost of memory safety.

Higher-level languages such as Java, C#, Python, Ruby, and JavaScript provide memory safety but offer less control over memory layout, and suffer from garbage collection pauses.

Rust is a modern, open source system programming language that promises the best of three worlds: the type safety of Java; the speed, expressiveness, and efficiency of C++; and memory safety without a garbage collector.

This book adopts a unique three-step approach to teaching system programming in Rust. Each chapter in this book starts with an overview of the system programming fundamentals and kernel system calls for that topic in Unix-like operating systems (Unix/Linux/macOS). You will then learn how to perform common system calls using the Rust Standard Library, and in a few cases, external crates, using abundant code snippets. This knowledge is then reinforced through a practical example project that you will build. Lastly, there are questions in each chapter to embed learning.

By the end of this book, you will have a sound foundational understanding of how to use Rust to manage and control operating system resources such as memory, files, processes, threads, system environment, peripheral devices, networking interfaces, terminals, and shells, and you'll understand how to build cross-language bindings through FFI. Along the way, you will learn how to use the tools of the trade, and get a firm appreciation of the value Rust brings to build safe, performant, reliable, and efficient system-level software.

Who this book is for

This book is aimed at programmers with basic knowledge of Rust but little or no system programming or experience. This book is also for people who have a background in system programming and want to consider Rust as an alternative to C/C++.

The reader should have a basic understanding of programming concepts in any language, such as C, C++, Java, Python, Ruby, JavaScript, or Go.

What this book covers

Chapter 1, Tools of the Trade – Rust Toolchains and Project Structures, introduces the Rust toolchain for build and dependency management, automated testing, and documentation.

Chapter 2, A Tour of the Rust Programming Language, illustrates the key concepts of the Rust programming language including the type system, data structures, and memory management fundamentals through an example project.

Chapter 3, Introduction to the Rust Standard Library, introduces key modules of the Rust standard library that provide the building blocks and pre-defined functionality for system programming in Rust.

Chapter 4, Managing Environment, Command Line, and Time, covers a few foundational topics around how to programmatically deal with command-line parameters, set and manipulate the process environment, and work with system time.

Chapter 5, Memory Management in Rust, provides a comprehensive look at the memory management facilities provided by Rust. We will review Linux memory management basics, the traditional shortcomings of C/C++, and how Rust can be used to overcome many of these shortcomings.

Chapter 6, Working with Files and Directories in Rust, helps you understand how the Linux filesystem works, and how to master the Rust Standard Library for various scenarios in file and directory operations.

Chapter 7, Implementing Terminal I/O in Rust, helps you understand how a pseudo-terminal application works and how to create one. The result will be an interactive application that handles streams.

Chapter 8, Working with Processes and Signals, provides an explanation of what processes are, how to handle them in Rust, how to create and communicate with a child process, and how to handle signals and errors.

Chapter 9, Managing Concurrency, explains the basics of concurrency and various mechanisms for sharing data across threads in an idiomatic way in Rust, including channels, mutexes, and reference counters.

Chapter 10, Working with Device I/O, explains Linux I/O concepts such as buffering, standard inputs and outputs, and device I/O, and shows how to control I/O operations with Rust.

Chapter 11, Learning Network Programming, explains how to work with low-level network primitives and protocols in Rust, illustrated by building low-level TCP and UDP servers and clients, and a reverse proxy.

Chapter 12, Writing Unsafe Rust and FFI, describes the key motivations and risks associated with unsafe Rust, and shows how to use FFI to safely interface Rust with other programming languages.

To get the most out of this book

Rustup must be installed in your local development environment. Use this link for installation: https://github.com/rust-lang/rustup.

Refer to the following link for official installation instructions: https://www.rust-lang.org/tools/install.

After installation, check whether rustc, and cargo have been installed correctly with the following commands:

rustc --version 
cargo –version

You can use Linux, macOS, or Windows.

While the Rust standard library largely is platform-independent, the general flavor of the book is oriented towards Linux/Unix-based systems. As a result, in a few chapters (or some sections within chapters) it is recommended to use a local Linux virtual machine, like Virtual box, (or if you have a cloud VM you may use it) for the code in the chapter to work. This may be because a command, or an external crate or a shared library used in example code and projects may be Linux/Unix specific.

Note for those using Windows for development

There are certain chapters that require a virtual machine or Docker image running Unix-like OSes (Unix/Linux/macOS).

There are two types of code in each chapter which are placed in the Packt GitHub repository for the book:

  • The code corresponding to the example projects (which are referred to by named source files within the chapter),
  • Independent code snippets, that are placed within the miscellaneous folder within each chapter (where applicable)

If you are using the digital version of this book, we advise you to type the code yourself or access the code via the GitHub repository (link available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

While using cargo run command to build and run Rust programs, you may encounter 'permission denied' messages if the user ID with which the command is run does not have sufficient permissions to perform system-level operations (such as reading or writing to files). In such cases, one of the workarounds that can be used is to run the program with the following command:

sudo env "PATH=$PATH" cargo run

Download the example code files

You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packt.com.
  2. Select the Support tab.
  3. Click on Code Downloads.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Practical-System-Programming-for-Rust-Developers. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Note

The code snippets in this book are designed for learning, and not intended to be of production quality. As a result, while the code examples are practical and use idiomatic Rust, they are not likely to be full-featured with robust error handling covering all types of edge cases. This is by design, so as not to impede the learning process.

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://static.packt-cdn.com/downloads/9781800560963_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "We can access the now() function from the Utc module to print out the current date and time."

A block of code is set as follows:

fn main() {
    println!("Hello, time now is {:?}", chrono::Utc::now());
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

fn main() {
    println!("Hello, time now is {:?}", chrono::Utc::now());
}

Any command-line input or output is written as follows:

rustup toolchain install nightly

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "You will see Hello, world! printed to your console."

Tips or important notes

Appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at customercare@packtpub.com.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packt.com with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packt.com.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Practical System Programming for Rust Developers
Published in: Dec 2020Publisher: PacktISBN-13: 9781800560963
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Prabhu Eshwarla

Prabhu Eshwarla has been shipping high-quality, business-critical software to large enterprises and running IT operations for over 25 years. He is also a passionate teacher of complex technologies. Prabhu has worked with Hewlett Packard and has deep experience in software engineering, engineering management, and IT operations. Prabhu is passionate about Rust and blockchain and specializes in distributed systems. He considers coding to be a creative craft, and an excellent tool to create new digital worlds (and experiences) sustained through rigorous software engineering.
Read more about Prabhu Eshwarla