Table Of ContentPractical
Rust Projects
Building Game, Physical Computing,
and Machine Learning Applications
—
Shing Lyu
ITeBooksFree.com
Practical Rust
Projects
Building Game, Physical
Computing, and Machine
Learning Applications
Shing Lyu
ITeBooksFree.com
Practical Rust Projects: Building Game, Physical Computing, and
Machine Learning Applications
Shing Lyu
Amsterdam, The Netherlands
ISBN-13 (pbk): 978-1-4842-5598-8 ISBN-13 (electronic): 978-1-4842-5599-5
https://doi.org/10.1007/978-1-4842-5599-5
Copyright © 2020 by Shing Lyu
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or
part of the material is concerned, specifically the rights of translation, reprinting, reuse of
illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way,
and transmission or information storage and retrieval, electronic adaptation, computer software,
or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark
symbol with every occurrence of a trademarked name, logo, or image we use the names, logos,
and images only in an editorial fashion and to the benefit of the trademark owner, with no
intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if
they are not identified as such, is not to be taken as an expression of opinion as to whether or not
they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of
publication, neither the authors nor the editors nor the publisher can accept any legal
responsibility for any errors or omissions that may be made. The publisher makes no warranty,
express or implied, with respect to the material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Steve Anglin
Development Editor: Matthew Moodie
Editorial Operations Manager: Mark Powers
Cover designed by eStudioCalamar
Cover image designed by Freepik (www.freepik.com)
Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York,
NY 10004, U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.
com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole
member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc).
SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected]; for reprint, paperback, or
audio rights, please email [email protected].
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our Print
and eBook Bulk Sales web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available
to readers on GitHub via the book’s product page, located at www.apress.com/9781484255988.
For more detailed information, please visit http://www.apress.com/source-code.
Printed on acid-free paper
ITeBooksFree.com
For my wife Wei-Chi, my father Ching-Chuan,
and my mother Man-Yun.
ITeBooksFree.com
Table of Contents
About the Author ���������������������������������������������������������������������������������xi
About the Technical Reviewer �����������������������������������������������������������xiii
Chapter 1: Welcome to the World of Rust ���������������������������������������������1
Who Is This Book For? �������������������������������������������������������������������������������������������2
Who Is This Book Not For? ������������������������������������������������������������������������������������3
Criteria for Selecting Libraries ������������������������������������������������������������������������������4
Pure Rust ���������������������������������������������������������������������������������������������������������4
Maturity �����������������������������������������������������������������������������������������������������������5
Popularity ��������������������������������������������������������������������������������������������������������5
How To Use This Book �������������������������������������������������������������������������������������������6
Chapter Overview ��������������������������������������������������������������������������������������������6
Source Code ����������������������������������������������������������������������������������������������������������7
Chapter 2: Building a Command-Line Program ������������������������������������9
Introduction �����������������������������������������������������������������������������������������������������������9
What Are You Building? ���������������������������������������������������������������������������������������10
Creating a Binary Project ������������������������������������������������������������������������������������11
Reading Command-Line Arguments with std::env::args �������������������������������������12
Handling Complex Arguments with StructOpt �����������������������������������������������������14
Adding Binary Flags (aka Switches) �������������������������������������������������������������������18
Printing to STDERR ����������������������������������������������������������������������������������������������19
Printing with Color ����������������������������������������������������������������������������������������������21
v
ITeBooksFree.com
TTaabbllee ooff CCoonnTTeennTTss
Reading the Cat Picture from a File ��������������������������������������������������������������������22
Better Error Handling ������������������������������������������������������������������������������������������26
Piping to Other Commands ���������������������������������������������������������������������������������31
Piping to STDOUT Without Color ��������������������������������������������������������������������32
Accepting STDIN ��������������������������������������������������������������������������������������������33
Integration Testing ����������������������������������������������������������������������������������������������34
Publishing and Distributing the Program ������������������������������������������������������������39
Install from Source ����������������������������������������������������������������������������������������39
Publish to crates�io ����������������������������������������������������������������������������������������40
Building Binaries for Distribution �������������������������������������������������������������������41
Conclusion ����������������������������������������������������������������������������������������������������������42
Chapter 3: Creating Graphical User Interfaces (GUIs) �������������������������43
What Are You Building? ���������������������������������������������������������������������������������������44
Building a Text-Based User Interface ������������������������������������������������������������������46
Showing a Dialog Box �����������������������������������������������������������������������������������������48
Handling Simple Keyboard Inputs �����������������������������������������������������������������������50
Adding a Dialog ���������������������������������������������������������������������������������������������������51
Multi-Step Dialogs ����������������������������������������������������������������������������������������������53
Reading User Input ����������������������������������������������������������������������������������������������56
Moving to Graphical User Interfaces (GUIs) ���������������������������������������������������������59
Creating a Window ����������������������������������������������������������������������������������������������60
Displaying an Image ��������������������������������������������������������������������������������������������63
Using Glade to Design the UI �������������������������������������������������������������������������������66
Accepting Inputs and Button Clicks ��������������������������������������������������������������������77
Reading a gtk::Switch �����������������������������������������������������������������������������������������82
Other Alternatives �����������������������������������������������������������������������������������������������85
vi
TTaabbllee ooff CCoonnTTeennTTss
Chapter 4: Building a Game ����������������������������������������������������������������87
What Are You Building? ���������������������������������������������������������������������������������������87
Amethyst and the Entity-Component-System Pattern ����������������������������������������88
Creating an Amethyst Project ������������������������������������������������������������������������������90
Creating a Window ����������������������������������������������������������������������������������������������92
Seeing the World Through a Camera �������������������������������������������������������������������97
Adding the Cats �������������������������������������������������������������������������������������������������100
Moving the Cats ������������������������������������������������������������������������������������������������109
Creating the Ball �����������������������������������������������������������������������������������������������117
Can’t Defy Gravity ���������������������������������������������������������������������������������������������121
Making the Ball Bounce ������������������������������������������������������������������������������������125
Keeping Score ���������������������������������������������������������������������������������������������������132
Let There Be Music �������������������������������������������������������������������������������������������142
Other Alternatives ���������������������������������������������������������������������������������������������153
Chapter 5: Physical Computing in Rust ��������������������������������������������155
What Are You Building? �������������������������������������������������������������������������������������156
Physical Computing on Raspberry Pi ����������������������������������������������������������������157
Getting to Know Your Raspberry Pi ��������������������������������������������������������������157
Install Raspbian Through NOOBS�����������������������������������������������������������������158
Install the Rust Toolchain �����������������������������������������������������������������������������160
Controlling the GPIO Pins �����������������������������������������������������������������������������161
Building a LED Circuit ����������������������������������������������������������������������������������163
Controlling the GPIO Output from Rust ��������������������������������������������������������166
Reading Button Clicks ���������������������������������������������������������������������������������170
vii
TTaabbllee ooff CCoonnTTeennTTss
Cross-Compiling to Raspberry Pi ����������������������������������������������������������������������177
How Does the GPIO Code Work? �����������������������������������������������������������������������179
Where Do You Go from Here? ����������������������������������������������������������������������������184
Chapter 6: Artificial Intelligence and Machine Learning ������������������187
What Is Machine Learning?�������������������������������������������������������������������������������187
Supervised vs� Unsupervised Learning �������������������������������������������������������188
What Are You Building? �������������������������������������������������������������������������������������189
Introducing the rusty-machine Crate ����������������������������������������������������������������191
Clustering Cat Breeds with K-Means ����������������������������������������������������������������193
Introduction to the K-Means Algorithm �������������������������������������������������������193
The Training Data �����������������������������������������������������������������������������������������195
Exporting as CSV �����������������������������������������������������������������������������������������200
Moving the Configuration Into a File ������������������������������������������������������������202
Visualizing the Data �������������������������������������������������������������������������������������207
Setting Up K-Means �������������������������������������������������������������������������������������211
Detecting Cats versus Dogs with the Neural Network ��������������������������������������218
Introduction to Neural Networks �����������������������������������������������������������������218
Preparing the Training and Testing Data ������������������������������������������������������221
Setting Up the Neural Network Model ���������������������������������������������������������224
Reading the Training and Testing Data ��������������������������������������������������������224
Normalizing the Training Data ���������������������������������������������������������������������227
Training and Predicting ��������������������������������������������������������������������������������230
Making the Prediction ���������������������������������������������������������������������������������232
Other Alternatives ���������������������������������������������������������������������������������������������234
viii
TTaabbllee ooff CCoonnTTeennTTss
Chapter 7: What Else Can You Do with Rust? �����������������������������������237
The End Is Just the Beginning ��������������������������������������������������������������������������237
The Web ������������������������������������������������������������������������������������������������������������237
Backend �������������������������������������������������������������������������������������������������������238
Frontend ������������������������������������������������������������������������������������������������������241
Web Browser and Crawler ���������������������������������������������������������������������������242
Mobile ����������������������������������������������������������������������������������������������������������244
Operating Systems and Embedded Devices ������������������������������������������������248
Unlimited Possibilities of Rust ���������������������������������������������������������������������249
Index �������������������������������������������������������������������������������������������������251
ix
About the Author
Shing Lyu has worked professionally on one of
the biggest Rust projects, the Firefox’s Gecko
engine, as well as on its upstream project, the
Servo browser engine. He is the co-creator
of RustPython, which has over 4.7k stars on
GitHub. Shing was also active in building the
local Rust community in Taiwan.
Besides Rust, Shing writes about all
sorts of programming topics on his blog at
https://shinglyu.com. He has also authored video courses, given talks
at conferences, and mentored other open source contributors. Shing has
worked for many companies, including Mozilla, Booking.com, Intel, and
DAZN.
xi