ebook img

Rust Programming By Example PDF

443 Pages·2018·3.98 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Rust Programming By Example

Guillaume Gomez, Antoni Boucher Rust Programming By Example Enter the world of Rust by building engaging, concurrent, reactive, and robust applications Rust Programming By Example (cid:38)(cid:79)(cid:85)(cid:70)(cid:83)(cid:2)(cid:85)(cid:73)(cid:70)(cid:2)(cid:88)(cid:80)(cid:83)(cid:77)(cid:69)(cid:2)(cid:80)(cid:71)(cid:2)(cid:51)(cid:86)(cid:84)(cid:85)(cid:2)(cid:67)(cid:90)(cid:2)(cid:67)(cid:86)(cid:74)(cid:77)(cid:69)(cid:74)(cid:79)(cid:72)(cid:2)(cid:70)(cid:79)(cid:72)(cid:66)(cid:72)(cid:74)(cid:79)(cid:72)(cid:14)(cid:2)(cid:68)(cid:80)(cid:79)(cid:68)(cid:86)(cid:83)(cid:83)(cid:70)(cid:79)(cid:85)(cid:14) (cid:83)(cid:70)(cid:66)(cid:68)(cid:85)(cid:74)(cid:87)(cid:70)(cid:14)(cid:2)(cid:66)(cid:79)(cid:69)(cid:2)(cid:83)(cid:80)(cid:67)(cid:86)(cid:84)(cid:85)(cid:2)(cid:66)(cid:81)(cid:81)(cid:77)(cid:74)(cid:68)(cid:66)(cid:85)(cid:74)(cid:80)(cid:79)(cid:84) Guillaume Gomez Antoni Boucher BIRMINGHAM - MUMBAI Rust Programming By Example Copyright (cid:97) 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(s), 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: Aaron Lazar Acquisition Editor: Alok Dhuri Content Development Editor: Akshada Iyer Technical Editor: Mehul Singh Copy Editor: Safis Editing Project Coordinator: Prajakta Naik Proofreader: Safis Editing Indexer: Pratik Shirodkar Graphics: Jason Monteiro Production Coordinator: Deepika Naik First published: January 2018 Production reference: 1090118 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78839-063-7 (cid:88)(cid:88)(cid:88)(cid:16)(cid:81)(cid:66)(cid:68)(cid:76)(cid:85)(cid:81)(cid:86)(cid:67)(cid:16)(cid:68)(cid:80)(cid:78) (cid:78)(cid:66)(cid:81)(cid:85)(cid:16)(cid:74)(cid:80) 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 (cid:88)(cid:88)(cid:88)(cid:16)(cid:49)(cid:66)(cid:68)(cid:76)(cid:85)(cid:49)(cid:86)(cid:67)(cid:16)(cid:68)(cid:80)(cid:78) and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at (cid:84)(cid:70)(cid:83)(cid:87)(cid:74)(cid:68)(cid:70)(cid:33)(cid:81)(cid:66)(cid:68)(cid:76)(cid:85)(cid:81)(cid:86)(cid:67)(cid:16)(cid:68)(cid:80)(cid:78) for more details. At (cid:88)(cid:88)(cid:88)(cid:16)(cid:49)(cid:66)(cid:68)(cid:76)(cid:85)(cid:49)(cid:86)(cid:67)(cid:16)(cid:68)(cid:80)(cid:78), 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 authors Guillaume Gomez is an open source lover (let's keep this simple). He's a reviewer for the Rust language and a member of the GNOME organization. Guillaume lives in Paris, France. I would like to thank Sebastian Dr(cid:96)ge for his review of the book. He did an amazing job in helping us improve the book. Antoni Boucher has been enjoying programming for 10 years, especially functional and system programming. He works in the ad tech industry and strives to improve the performance and reliability of software. He contributes to multiple open source projects and is interested in system programming and compilers. Antoni lives in Montreal, Canada. About the reviewers Sebastian Dr(cid:96)ge is a free software developer, currently working for Centricular Ltd. His main involvement is with the GStreamer project, a cross-platform multimedia framework. He also contributes to various other projects, such as Debian, GNOME, Rust, and WebKit. He works as a contractor on free software. Thanks to the authors for making the Rust programming language accessible to more people with this book and for offering me to review this book, and to the Rust team for creating such a useful and usable language with great documentation. Daniel Durante is an avid coffee drinker/roaster, motorcyclist, archer, welder, and carpenter whenever he isn't programming. From the age of 12, he has been involved with web and embedded programming with PHP, Node.js, Golang, Rust, and C. He has worked on text-based browser games that have reached over 1,000,000 active players and created bin-packing software for CNC machines. He loves working with embedded programming with cortex-m and PIC circuits, high-frequency trading applications, and he has helped contribute to one of the oldest ORMs of Node.js (SequelizeJS). I would like to thank my parents, my brother, and friends who've all put up with my insanity sitting in front of a computer day in and day out. I would not be here today if it wasn't for their patience, guidance, and love. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit (cid:66)(cid:86)(cid:85)(cid:73)(cid:80)(cid:83)(cid:84)(cid:16)(cid:81)(cid:66)(cid:68)(cid:76)(cid:85)(cid:81)(cid:86)(cid:67)(cid:16)(cid:68)(cid:80)(cid:78) 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. Table of Contents Preface 1 Chapter 1: Basics of Rust 6 Getting to know Rust 7 Installing Rust 8 Windows 8 Linux/Mac 8 Test your installation 10 Documentation and reference 11 Main function 11 Variables 11 Built-in data types 12 Integer types 13 Floating-point types 13 Boolean type 13 Character type 13 Control flow 14 Writing a condition 14 Creating while loops 14 Creating functions 15 Creating structures 15 References 17 Clone types 18 Copy types 19 Mutable references 20 Methods 20 Constructors 21 Tuples 22 Enumerations 22 Pattern matching 23 Irrefutable patterns 25 Traits 25 Default methods 27 Associated types 27 Rules 28 Table of Contents Generics 29 The Option type 29 Arrays 30 Slices 30 For loops 31 Macros 33 Multiple pattern rules 34 Repetitions 35 Optional quantifier 37 Summary 38 Chapter 2: Starting with SDL 39 Understanding Rust crates 39 Installing SDL2 39 Installing SDL2 on Linux 39 Installing SDL2 on Mac 40 Installing SDL2 on Windows 40 Windows with Build Script 40 Windows (MinGW) 42 Windows (MSVC) 43 Setting up your Rust project 44 Cargo and crates.io 45 The docs.rs documentation 47 Back to our Cargo.toml file 48 Rust's modules 49 Tetris 51 Creating a window 52 Drawing 54 Playing with Options 60 Solution 61 Loading images 64 Installing SDL2_image on Mac 64 Installing SDL2_image on Linux 64 Installing SDL2_image on Windows 65 Playing with features 65 Playing with images 66 Handling files 69 Saving/loading high scores 72 Iterators 72 Reading formatted data from files 74 Summary 77 [ ii ] Table of Contents Chapter 3: Events and Basic Game Mechanisms 78 Writing Tetris 78 Tetrimino 79 Creating tetriminos 83 Generating a tetrimino 89 Rotating a tetrimino 91 Tetris struct 95 Interacting with the game map 99 SDL events 102 Score, level, lines sent 110 Levels and lines sent 113 Highscores loading/overwriting 114 Summary 128 Chapter 4: Adding All Game Mechanisms 129 Getting started with game mechanisms 129 Rendering UI 129 Rendering initialization 130 Rendering 133 Playing with fonts 140 Install on OS X 141 Install on Linux 141 Other system/package manager 141 Loading font 142 Summary 147 Chapter 5: Creating a Music Player 148 Installing the prerequisite 148 Installing GTK+ on Linux 149 Installing GTK+ on Mac 149 Installing GTK+ on Windows 149 Creating your first window 149 Closure 152 Preventing the default behavior of an event 153 Creating a toolbar 153 Stock item 155 Improving the organization of the application 156 Adding tool button events 159 Lifetime 161 Ownership 165 Containers 166 [ iii ] Table of Contents Types of containers 166 The Box container 167 Adding a playlist 169 The MVC pattern 171 Opening MP3 files 175 Reference-counting pointer 176 ID3— MP3 metadata 177 Opening files with a file dialog 178 Deleting a song 180 Displaying the cover when playing a song 181 Summary 182 Chapter 6: Implementing the Engine of the Music Player 183 Installing the dependencies 184 Installing dependencies on Linux 184 Installing dependencies on Mac 184 Installing dependencies on Windows 184 Decoding MP3 files 185 Adding dependencies 185 Implementing an MP3 decoder 185 Getting the frame samples 189 Playing music 190 Event loop 191 Atomic reference counting 192 Mutual exclusion 193 Send trait 193 Sync trait 193 Lock-free data structures 193 Playing music 196 Mutex guard 198 RAII 198 Using the music player 200 Pausing and resuming the song 201 Interior mutability 202 Showing the progression of the song 209 Improving CPU usage 213 Condition variable 214 Showing the song's current time 216 Loading and saving the playlist 220 Saving a playlist 220 Loading a playlist 223 [ iv ]

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.