Mastering Rust
Second Edition
Learn about memory safety, type system, concurrency, and the new features of Rust 2018 edition
Rahul Sharma
Vesa Kaihlavirta
BIRMINGHAM - MUMBAI
Mastering RustSecond Edition
Copyright 2019 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 or its dealers and distributors, will be held liable for any damages caused or alleged to have been 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.
Commissioning Editor: Richa Tripathi
Acquisition Editor: Denim Pinto
Content Development Editor: Anugraha Arunagiri
Technical Editor: Aniket Iswalkar
Copy Editor: Safis Editing
Project Coordinator: Ulhas Kambali
Proofreader: Safis Editing
Indexer: Rekha Nair
Graphics: Tom Scaria
Production Coordinator: Nilesh Mohite
First published: May 2017
Second published: January 2019
Production reference: 1310119
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78934-657-2
www.packtpub.com
mapt.io
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Why subscribe?
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
Packt.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.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at customercare@packtpub.com for more details.
At www.packt.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.
Contributors
About the author
Rahul Sharma is passionately curious about teaching programming. He has been writing software for the last two years. He got started with Rust with his work on Servo, a browser engine by Mozilla Research as part of his GSoC project. At present, he works at AtherEnergy, where he is building resilient cloud infrastructure for smart scooters. His interests include systems programming, distributed systems, compilers and type theory. He is also an occasional contributor to the Rust language and does mentoring of interns on the Servo project by Mozilla.
Vesa Kaihlavirta has been programming since he was five, beginning with C64 Basic. His main professional goal in life is to increase awareness of programming languages and software quality in all industries that use software. He's an Arch Linux Developer Fellow, and has been working in the telecom and financial industry for a decade. Vesa lives in Jyvaskyla, central Finland.
About the reviewer
Gaurav Aroraa has an M.Phil in computer science. He is a Microsoft MVP; a lifetime member of Computer Society of India; an advisory member of IndiaMentor; and certified as a Scrum trainer/coach, XEN for ITIL-F, and APMG for PRINCE-F and PRINCE-P. He is an open source developer, a contributor to TechNet Wiki, and the founder of Ovatic Systems Private Limited. In over 20 years of his career, he has mentored thousands of students and industry professionals. You can tweet Gaurav on his Twitter handle at @g_arora.
To my wife, Shuby Arora, and my daughter, Aarchi Arora, an angel, who permitted me to steal time for this book from the time I was supposed to spend with them. Thanks to the entire Packt team, especially Ulhas and Anugraha Arunagiri, whose coordination and communication during the period was tremendous, and Denim Pinto, who introduced me to this book.
Packt is searching for authors like you
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Preface
This book is about Rust, a programming language that empowers you to build all kinds of software systems, ranging from low-level embedded software to dynamic web applications. Rust is fast, reliable, and safe. It offers performance and safety guarantees that reach or even surpass C and C++, while still being a modern language with a relatively low barrier of entry. Rust drive toward incremental improvements, combined with its active and friendly community, promises a great future for the language.
Rust is not a new language by design and doesn't try to reinvent the wheel. Rather, it's a language that has identified unique ideas, hidden away in research-prototype languages that never saw mass adoption. It brings those ideas together into a coherent composition and provides a practical language that lets you build safe software systems, while still being efficient.
Who this book is for
This book targets both beginner and intermediate programmers familiar with other imperative languages, but new to Rust. It assumes that you are familiar with at least one imperative programming language, such as C, C++, or Python. Knowing about functional programming is not a requirement, but it's good to have a general idea about it. We make sure to explain any concept or idea that we introduce from these languages, though.
What this book covers
, Getting Started with Rust , gives a brief history on Rust and the motivation behind its design, and covers basic language syntax. The chapter ends with an exercise covering all the language features.
Next page