Rust Essentials
Second Edition
A quick guide to writing fast, safe, and concurrent systems and applications
Ivo Balbaert
BIRMINGHAM - MUMBAI
Rust Essentials
Second Edition
Copyright 2017 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: May 2015
Second edition: November 2017
Production reference: 1061117
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78839-001-9
www.packtpub.com
Credits
Author Ivo Balbaert | Copy Editor Safis Editing |
Reviewer Tom Verbesselt | Project Coordinator Vaidehi Sawant |
Commissioning Editor Merint Mathew | Proofreader Safis Editing |
Acquisition Editor Karan Sadawana | Indexer Tejal Daruwale Soni |
Content Development Editor Rohit Kumar Singh | Graphics Abhinash Sahu |
Technical Editor Ruvika Rao | Production Coordinator Melwyn Dsa |
About the Author
Ivo Balbaert is currently a lecturer in (web) programming and databases at CVO Antwerpen, a community college in Belgium. He received a PhD in applied physics from University of Antwerp in 1986. He has worked for 20 years in the software industry as a developer and consultant in several companies, and he has worked for 10 years as a project manager at the University Hospital of Antwerp. From 2000 onward, he switched to teaching, developing software, and writing technical books.
About the Reviewer
Tom Verbesselt started his career as a software engineer in the amazing early world of 3D scanning and printing. However, he couldn't ignore his passion for teaching, and so he became a lecturer at the graduate program at CVO Antwerpen (Antwerp, Belgium). He started his own IT consultancy firm in 2003 and cofounded OWIC, a non-profit organization dedicated to teaching open source software tools, in 2016. Currently, he is the head of the graduate program in IT at CVO Antwerpen, and he is passionate about mobile, web, and new technology.
I have known Ivo for more than 10 years as a colleague at CVO Antwerpen. He is a born teacher and always willing to share his knowledge and insights. He is passionate about programming, and this book will give you all that you need to get started in Rust.
This book is a well guided tour around the Rust programming language. I was amazed by the speed, elegance, and possibilities of this programming language. After reading this book, you will have a solid foundation in the language and the advantages and possibilities that it offers. I've loved every page of it, and I hope you will do too.
www.PacktPub.com
For support files and downloads related to your book, please visit www.PacktPub.com .
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details.
At www.PacktPub.com , you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Why subscribe?
- Fully searchable across every book published by Packt
- Copy and paste, print, and bookmark content
- On demand and accessible via a web browser
Customer Feedback
Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/ 1788390016 .
If you'd like to join our team of regular reviewers, you can e-mail us at customerreviews@packtpub.com. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!
Preface
Rust is a stable, open source, and compiled programming language that finally promises software developers the utmost safety--not only type safety, but also memory safety. The compiler carefully checks all uses of variables and pointers so that common problems from C/C++ and other languages, such as pointers to wrong memory locations or null references, are a thing of the past. Possible problems are detected at compilation time, and Rust programs execute at speeds comparable with their C++ counterparts.
Rust runs with a very light runtime, which does not perform garbage collection. Again, the compiler takes care of generating the code that frees all resources at the right time. This means Rust can run in very constrained environments, such as embedded or real-time systems. The built-in safety also guarantees concurrency without data-race problems.
It is clear that Rust is applicable in all use cases where until now C and C++ were the preferred languages and that it will do a better job at it, at least with regard to safety and robustness.
Rust is also a very rich language: it has concepts (such as immutability by default) and constructs (such as traits) that enable developers to write code in a high-level functional and object-oriented style.
The original goal of Rust was to serve as the language for writing a new safe browser engine, devoid of the many security flaws that plague existing browsers such as the Servo project from Mozilla Research.
The goal of this book is to give you a firm foundation for starting to develop in Rust. Throughout the book, we emphasize the three pillars of Rust: safety, performance, and sound concurrency. We will discuss where Rust differs from other programming languages and why this is the case. The code examples are not chosen ad hoc, but they are oriented as part of an ongoing project for building a game so that there is a sense of cohesion and evolution in the examples.
Next page