Hands-On Functional Programming in Rust
Build modular and reactive applications with functional programming techniques in Rust 2018
Andrew Johnson
BIRMINGHAM - MUMBAI
Hands-On Functional Programming in Rust
Copyright 2018 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: Karan Sadawana
Content Development Editor: Tiksha Sarang
Technical Editor: Adhithya Haridas
Copy Editor: Safis Editing
Project Coordinator: Prajakta Naik
Proofreader: Safis Editing
Indexer: Rekha Nair
Graphics: Jisha Chirayil
Production Coordinator: Shantanu Zagade
First published: May 2018
Production reference: 1300518
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78883-935-8
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
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.
Contributors
About the author
Andrew Johnson is a software developer who has worn many hats. Professionally, he has worked on projects written in C, C++, Java, Python, Ruby, JavaScript, Haskell, OCaml, and now Rust. Most notably, he has worked as an early employee at Topsy Labs (acquired by Apple) and FiscalNote (growing rapidly). Academically, his interests are focused on the intersection between formal language processing (such as programming languages) and existing natural language programming techniques.
About the reviewer
Sebastian Drge is a free software developer currently working with Centricular Ltd. He has been involved for more than 10 years with the GStreamer project, a cross-platform multimedia framework. He also contributes to various other projects, such as Debian, GNOME, Rust, and WebKit. While finishing his master's degree in computer science, he started working as a contractor on free software and continues to do so to this day.
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
Thanks for your interest in functional programming in Rust. Rust is a very young programming language and is particularly new to the functional programming community. Despite its age, the language provides a wealth of tools that are both practical and sophisticated.
In this book, we will introduce general functional programming principles and how they apply to Rust specifically. Our goal is to provide knowledge and a perspective on Rust that will outlast small changes to language features. The pace of development of Rust is so fast that during the course of writing the book we introduced new features as they became available and relevant. We want to equip the reader to produce code for this fast-moving environment such that they are prepared to best utilize new features as they are released.
Who this book is for
This book is for developers who are familiar with basic Rust features or are willing to reference other material as they read along. We will not fully explain every new symbol, library, or syntax form, but we do explain libraries that are considered more advanced or syntax that may be difficult to read. Similarly, some concepts that are only briefly explained in the introductory material will be explained in detail.
What this book covers
, Functional Programming a Comparison , introduces functional programming in Rust. Comparisons are drawn between functional style and other paradigms that are prevalent or influential to Rust. The chapter also serves as a brief outline of topics that will appear later in the book.
, Functional Control Flow , introduces Rust control flow structures while explaining how they are relevant to the functional style of programming. The expression-centric nature of functional programming and Rust is illustrated through examples. Limiting as it may be, the chapter also begins an ongoing project using only the procedural expression style of programming.
, Functional Data Structures , introduces the reader to the various, highly expressive data types available in Rust. Notably, the enum type is introduced, which holds particular significance in functional programming. The project continues to grow to incorporate a variety of these data types.
, Generics and Polymorphism , explains the concepts of parameterization of data (generics) and parameterization of control flow (polymorphism). Parameterization and its natural interaction with traits reduces the programmer's burden, but the syntax can become overwhelming. Some approaches to reduce or mitigate parameter explosion are introduced. The ongoing project again grows to incorporate these features.
Next page