Lyu Shing, Rzeznik Andrew - Practical Rust Projects. Build Serverless, AI, Machine Learning, Embedded, Game, And Web Applications (Second Edition).pdf

(6278 KB) Pobierz
Practical Rust
Projects
Build Serverless, AI, Machine Learning,
Embedded, Game, and Web Applications
Second Edition
Shing Lyu
Andrew Rzeznik
Practical Rust Projects
Build Serverless, AI, Machine
Learning, Embedded, Game, and
Web Applications
Second Edition
Shing Lyu
Andrew Rzeznik
Practical Rust Projects: Build Serverless, AI, Machine Learning, Embedded, Game,
and Web Applications
Shing Lyu
Amsterdam, The Netherlands
ISBN-13 (pbk): 978-1-4842-9330-0
https://doi.org/10.1007/978-1-4842-9331-7
Andrew Rzeznik
Arlington, MA, USA
ISBN-13 (electronic): 978-1-4842-9331-7
Copyright © 2023 by Shing Lyu and Andrew Rzeznik
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: James Markham
Editorial Assistant: Gryffin Winkler
Copy Editor: April Rondeau
Cover designed by eStudioCalamar
Cover image designed by xb100 on freepik
Distributed to the book trade worldwide by Springer Science+Business Media New York, 1 New York Plaza,
Suite 4600, New York, NY 10004-1562, USA. Phone 1-800-SPRINGER, fax (201) 348-4505, email 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 booktranslations@springernature.com; for reprint, paperback,
or audio rights, please e-mail bookpermissions@springernature.com.
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 (github.com/apress). For more detailed information, please visit http://www.apress.com/
source-code.
Printed on acid-free paper
Table of Contents
About the Authors�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½ ix
About the Technical Reviewer �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½ xi
Introduction �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½ xiii
Chapter 1: Welcome to the World of Rust �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½ 1
1.1 Who Is This Book For? ............................................................................................................. 2
1.2 Who Is This Book Not For? ...................................................................................................... 3
1.3 Criteria for Selecting Libraries ................................................................................................ 3
Pure Rust ................................................................................................................................. 4
Maturity ................................................................................................................................... 4
Popularity ................................................................................................................................ 5
1.4 How to Use This Book ............................................................................................................. 5
Chapters Overview .................................................................................................................. 5
1.5 Source Code ............................................................................................................................ 7
1.6 Topics Not Covered.................................................................................................................. 8
Chapter 2: Building a Command-line Program �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½ 9
2.1 What Are You Building? ......................................................................................................... 10
2.2 Creating a Binary Project ...................................................................................................... 11
2.3 Reading Command-line Arguments with
std::env::args
................................................ 12
2.4 Handling Complex Arguments with
Clap..............................................................................
13
2.5 Adding Binary Flags .............................................................................................................. 17
2.6 Printing to STDERR................................................................................................................ 18
2.7 Printing with Color ................................................................................................................ 19
2.8 Reading the Cat Picture from a File ...................................................................................... 21
2.9 Handling Errors ..................................................................................................................... 24
iii
Table of ConTenTs
2.10 Piping to Other Commands ................................................................................................. 27
Piping to STDOUT Without Color ............................................................................................ 28
Accepting STDIN .................................................................................................................... 29
2.11 Integration Testing .............................................................................................................. 30
2.12 Publishing and Distributing the Program ............................................................................ 34
Install from Source ................................................................................................................ 34
Publish to crates.io ................................................................................................................ 35
Building Binaries for Distribution........................................................................................... 36
2.13 Conclusion .......................................................................................................................... 38
Chapter 3: Creating Graphical User Interfaces (GUIs)�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½ 39
3.1 What Are You Building? ......................................................................................................... 40
3.2 Building a Text-based User Interface .................................................................................... 41
3.3 Showing a Dialog Box ........................................................................................................... 43
3.4 Handling Simple Keyboard Inputs ......................................................................................... 45
3.5 Adding a Dialog ..................................................................................................................... 46
3.6 Multi-step Dialogs ................................................................................................................. 47
3.7 Reading User Input................................................................................................................ 50
3.8 Moving to Graphical User Interfaces (GUIs)........................................................................... 52
3.9 Creating a Window ................................................................................................................ 53
3.10 Displaying an Image............................................................................................................ 55
3.11 Using Glade to Design the UI ............................................................................................... 58
3.12 Accepting Inputs and Button Clicks .................................................................................... 68
3.13 Reading a
gtk::Switch
.................................................................................................... 72
3.14 Alternatives ......................................................................................................................... 75
3.15 Conclusion .......................................................................................................................... 77
iv
Zgłoś jeśli naruszono regulamin