Preface
Rust is a new programming language. It offers performance and safety that is equivalent to, or even surpasses, modern C++ while being a modern language with a relatively low barrier to entry. Rust's momentum, combined with its active and friendly community, promise a great future for the language.
While modern and fluent, Rust is not a particularly easy language. The memory management system keeps track of the life of every entity that is used in your program and is designed in such a way that this tracking can typically happen entirely at compile time. The Rust programmer's burden is to help the compiler when it cannot decide for itself what should happen. Since modern programming is possible without ever facing such responsibilities, a modern programmer may not immediately feel comfortable with Rust.
However, like all expertise and skill, the more difficult it is to attain, the more valuable it is, and this book is here to help you. This book covers the basics of Rust, enabling you to gain enough skills to start programming with it.
What this book covers
Chapter 1, Introducing and Installing Rust, deals with installing the Rust toolset and using the basic tools.
Chapter 2, Variables, focuses on using different kinds of variables.
Chapter 3, Input and Output, covers the basic I/O.
Chapter 4, Conditions, Recursion, and Loops, goes through the different loops and iterative methods of Rust.
Chapter 5, Remember, Remember, covers Rust's memory handling system.
Chapter 6, Creating Your Own Rust Applications, gives you the task of building a complete Rust application.
Chapter 7, Matching and Structures, teaches you compound data types and how to destructure them.
Chapter 8, The Rust Application Lifetime, covers Rust's unique ownership, borrowing, and lifetime system, which enables resource safety without garbage collection.
Chapter 9, Introducing Generics, Impl, and Traits, goes through Rust's generic types.
Chapter 10, Creating Your Own Crate, instructs you how to build your own contained packages of Rust code.
Chapter 11, Concurrency in Rust, looks at concurrency and parallelism techniques.
Chapter 12, Now It's Your Turn!, gives you another set of tasks to complete.
Chapter 13, The Standard Library, covers Rust's standard library.
Chapter 14, Foreign Function Interfaces, introduces techniques to interface Rust code with C programs.
What you need for this book
To really dive into the content of this book, you should write out the example code and do the exercises. For that, you'll need a fairly recent computer; a gigabyte of RAM should be enough for the purposes of this book, but the more you have the faster the builds will be.
Linux is the best supported operating system here, but Rust itself is a first-class citizen on macOS and recent versions of Windows, so all the examples should adapt well there.
Who this book is for
This book will appeal to application developers who would like to build applications with Rust. No knowledge of programming is required.
Conventions
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning. Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "A shorter form is available in the unwrap
method. This is the same as the expect
method, but it doesn't print out anything in case of a failure."
A block of code is set as follows:
let mut file = File::create("myxml_file.xml).unwrap(); let mut output = io::stdout(); let mut input = io::stdin();
Any command-line input or output is written as follows:
cd app_name cargo build app_name
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "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)."
Note
Warnings or important notes appear like this.
Note
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply email [email protected]
, and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files emailed directly to you. You can download the code files by following these steps:
- Log in or register to our website using your email address and password.
- Hover the mouse pointer on the
SUPPORT
tab at the top. - Click on
Code Downloads & Errata
. - Enter the name of the book in the
Search
box. - Select the book for which you're looking to download the code files.
- Choose from the drop-down menu where you purchased this book from.
- Click on
Code Download
.
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/Learning-Rust. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form
link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title. To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata
section.
Piracy
Piracy of copyrighted material on the internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the internet, please provide us with the location address or website name immediately so that we can pursue a remedy. Please contact us at [email protected]
with a link to the suspected pirated material. We appreciate your help in protecting our authors and our ability to bring you valuable content.
Questions
If you have a problem with any aspect of this book, you can contact us at [email protected]
, and we will do our best to address the problem.