ebook img

The Complete Rust Programming Reference Guide: Design, develop, and deploy effective software systems using the advanced constructs of Rust PDF

685 Pages·2019·11.165 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 The Complete Rust Programming Reference Guide: Design, develop, and deploy effective software systems using the advanced constructs of Rust

The Complete Rust Programming Reference Guide Design, develop, and deploy effective software systems using the advanced constructs of Rust Rahul Sharma Vesa Kaihlavirta Claus Matzinger BIRMINGHAM - MUMBAI The Complete Rust Programming Reference Guide 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 authors, 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. First published: May 2019 Production reference: 1200519 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-83882-810-3 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 [email protected] 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 authors 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. Claus Matzinger is a software engineer with a very diverse background. After working in a small company maintaining code for embedded devices, he joined a large corporation to work on legacy Smalltalk applications. This led to a great interest in programming languages early on, and Claus became the CTO for a health games start-up based on Scala technology. Since then, Claus' roles have shifted toward customer-facing roles in the IoT database technology start-up crate.io and, most recently, Microsoft. There, he hosts a podcast, writes code together with customers, and blogs about the solutions arising from these engagements. For more than 5 years, Claus has implemented software to help customers innovate, achieve, and maintain success. 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. Table of Contents Preface 1 Chapter 1: Getting Started with Rust 7 What is Rust and why should you care? 8 Installing the Rust compiler and toolchain 15 Using rustup.rs 15 A tour of the language 18 Primitive types 21 Declaring variables and immutability 22 Functions 23 Closures 25 Strings 26 Conditionals and decision making 27 Match expressions 29 Loops 30 User-defined types 32 Structs 33 Enums 35 Functions and methods on types 37 Impl blocks on structs 38 Impl blocks for enums 40 Modules, imports, and use statements 42 Collections 42 Arrays 43 Tuples 43 Vectors 44 Hashmaps 44 Slices 45 Iterators 47 Exercise – fixing the word counter 48 Summary 49 Chapter 2: Managing Projects with Cargo 50 Package managers 51 Modules 51 Nested modules 52 File as a module 54 Directory as module 55 Cargo and crates 57 Creating a new Cargo project 59 Cargo and dependencies 61 Table of Contents Running tests with Cargo 63 Running examples with Cargo 66 Cargo workspace 66 Extending Cargo and tools 68 Subcommands and Cargo installation 68 cargo-watch 68 cargo-edit 69 cargo-deb 69 cargo-outdated 70 Linting code with clippy 70 Exploring the manifest file – Cargo.toml 71 Setting up a Rust development environment 74 Building a project with Cargo – imgtool 78 Summary 81 Chapter 3: Tests, Documentation, and Benchmarks 82 Motivation for testing 83 Organizing tests 84 Testing primitives 84 Attributes 85 Assertion macros 86 Unit tests 87 First unit test 87 Running tests 88 Isolating test code 88 Failing tests 90 Ignoring tests 90 Integration tests 91 First integration test 92 Sharing common code 93 Documentation 95 Writing documentation 95 Generating and viewing documentation 96 Hosting documentation 96 Doc attributes 97 Documentation tests 98 Benchmarks 99 Built-in micro-benchmark harness 100 Benchmarking on stable Rust 102 Writing and testing a crate – logic gate simulator 105 Continuous integration with Travis CI 109 Summary 113 Chapter 4: Types, Generics, and Traits 114 Type systems and why they matter 114 Generics 116 [ ii ] Table of Contents Creating generic types 117 Generic functions 118 Generic types 119 Generic implementations 119 Using generics 121 Abstracting behavior with traits 123 Traits 124 The many forms of traits 130 Marker traits 130 Simple traits 131 Generic traits 131 Associated type traits 131 Inherited traits 132 Using traits with generics – trait bounds 132 Trait bounds on types 136 Trait bounds on generic functions and impl blocks 137 Using + to compose traits as bounds 138 Trait bounds with impl trait syntax 139 Exploring standard library traits 141 True polymorphism using trait objects 148 Dispatch 148 Trait objects 149 Summary 151 Chapter 5: Memory Management and Safety 152 Programs and memory 153 How do programs use memory? 155 Memory management and its kinds 156 Approaches to memory allocation 157 The stack 157 The heap 159 Memory management pitfalls 161 Memory safety 161 Trifecta of memory safety 164 Ownership 164 A brief on scopes 166 Move and copy semantics 168 Duplicating types via traits 169 Copy 170 Clone 170 Ownership in action 172 Borrowing 177 Borrowing rules 179 Borrowing in action 180 Method types based on borrowing 182 Lifetimes 183 [ iii ] Table of Contents Lifetime parameters 184 Lifetime elision and the rules 185 Lifetimes in user defined types 187 Lifetime in impl blocks 187 Multiple lifetimes 188 Lifetime subtyping 188 Specifying lifetime bounds on generic types 189 Pointer types in Rust 191 References – safe pointers 192 Raw pointers 192 Smart pointers 193 Drop 193 Deref and DerefMut 195 Types of smart pointers 195 Box<T> 196 Reference counted smart pointers 198 Rc<T> 199 Interior mutability 204 Cell<T> 204 RefCell<T> 206 Uses of interior mutability 207 Summary 209 Chapter 6: Error Handling 210 Error handling prelude 210 Recoverable errors 213 Option 213 Result 217 Combinators on Option/Result 221 Common combinators 222 Using combinators 223 Converting between Option and Result 225 Early returns and the ? operator 225 Non-recoverable errors 227 User-friendly panics 231 Custom errors and the Error trait 231 Summary 236 Chapter 7: Advanced Concepts 237 Type system tidbits 237 Blocks and expressions 238 Let statements 241 Loop as an expression 246 Type clarity and sign distinction in numeric types 247 Type inference 249 Type aliases 251 Strings 252 [ 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.