Rust Cookbook (Vigneshwer Dhinakaran) (z-library.sk, 1lib.sk, z-lib.sk)

Author: Vigneshwer Dhinakaran

RUST

No Description

📄 File Format: PDF
💾 File Size: 4.9 MB
11
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
(This page has no text content)
📄 Page 2
Rust Cookbook Understand the core concepts of rust such as, memory management, concurrency, packaging, and many more Vigneshwer Dhinakaran BIRMINGHAM - MUMBAI
📄 Page 3
Rust Cookbook Copyright © 2017 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, and its dealers and distributors will be held liable for any damages caused or alleged to be 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: July 2017 Production reference: 1260717 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78588-025-4 www.packtpub.com
📄 Page 4
Credits Author Vigneshwer Dhinakaran Copy Editor Muktikant Garimella Gladson Monteiro Reviewer Pradeep R Project Coordinator Ulhas Kambali Commissioning Editor Aaron Lazar Proofreader Safis Editing Acquisition Editor Denim Pinto Indexer Rekha Nair Content Development Editor Vikas Tiwari Graphics Abhinash Sahu Technical Editor Diwakar Shukla Production Coordinator Melwyn Dsa
📄 Page 5
About the Author Vigneshwer Dhinakaran is an innovative data scientist with an artistic perception of technology and business, having over 3 years of experience in various domains, such as IOT, DevOps, computer vision, and deep learning, and is currently engaged as a research analyst crunching real-time data and implementing state-of-the-art AI algorithms in the innovation and development lab of the world’s largest decision science company in Bengaluru, India. He is an official Mozilla representative and Techspeaker in India and has been associated with Mozilla communities and technologies for more than 5 years. He has delivered various sessions on Rust language at many meetups and conferences; some of the highlighted events are Hong Kong Open Source Conference, FOSSMeet 17, and RainOfRust Campaign. He has played a key role in the formation and growth of the Rust community in India and was part of the Mozilla Reps mobilizer experiment, where he researched about the roadblocks and solutions to drive developers to adopt the Rust language in India.
📄 Page 6
Acknowledgments I would like to thank the entire Packt team for providing me with all their support and guidance throughout publishing my first book. I would like to call out Mr.Vikas Tiwari, who is the content development editor of the book, for patiently handling all my queries and planning the entire project; it has been a great experience working with you. Thanks to my college, Rajagiri School of Engineering and Technology, Kochi, from where I got my bachelor's degree. This place provided me with a great platform to learn, explore, and practice various engineering and leadership skills. I would like to express my sincere thanks to my professor, Dr. Deepti Das Krishna, who has always guided me in the right direction and supported all my activities. Thanks to all my teachers who have taught me so many important lessons throughout my life. Thanks to Mr. Pradeep Ramalingam, the technical reviewer of the book and my mentor for providing support, offering comments, and assisting me during the entire editing, proofreading, and design of the book. Thanks to Mr. Nitish Bhardwaj, who has always offered me a lot of mentorship and guidance to develop as an engineering professional in the early days of my career. Being part of the Mozilla community is one the best things that has happened to me. I wouldn’t have been here without the amazing exposure and learning experience from the network. I would like to express my gratitude to the many people who have helped me grow as a contributor in the ecosystem, and special thanks to the Mozilla Reps, Mozilla Tech Speakers, and Mozilla India community volunteers. Thanks also to the Rust community team and members for their amazing work and contribution in providing developers with great documentation, and inclusive practices and programs; it has truly been a great learning experience being part of the community. Thanks to my longtime friends, Harry Prince, Jairam Sankar, Srinivas Srikanth, Sriram Subramanian, Krishna Prasad, Firoz Jamal, and Ashique MN, and to all well wishers for their support and friendship.
📄 Page 7
Above all, I want to thank my parents, D.Dhinakaran and D.Selvi, for providing me with all the right resources and for being a great source of encouragement and positivity in my life, to my younger sister D.Abhinaya for showing great confidence in my works, and to the rest of my family for their unconditional love and support, inspite of all the time it took me away from them. It was a long and difficult journey for them. Last but not least, I beg forgiveness of all those who have been with me over the course of the years and whose names I have failed to mention.
📄 Page 8
About the Reviewer Pradeep R is an avid programmer who is passionate in working on network security. He is an experienced developer working on network security and network monitoring/visibility platforms devices for the past 5 years. He has worked on areas such as NAT, Firewall, VPN, Intrusion Detection Systems, network switching, and routing solutions. He loves different programming languages and strongly believes that all programming languages are similar in essence and can be easily adapted. His area of interest spans over different programming languages and extensively works with C, C++, Python, JavaScript, Perl, and occasionally with Java, .NET, and Rust. He is currently working as a lead engineer in Gigamon Inc. on network visibility devices. The Gigamon Inc. manufactures network visibility next generation devices that are used for analyzing the network traffic and monitors them to detect malicious activity or determine abnormal network usage pattern to detect security breach.
📄 Page 9
www.PacktPub.com For support files and downloads related to your book, please visit www.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.comand 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. h t t p s ://w w w . p a c k t p u b . c o m /m a p t Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career. Why subscribe? Fully searchable across every book published by Packt Copy and paste, print, and bookmark content On demand and accessible via a web browser
📄 Page 10
Customer Feedback Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at h t t p s ://w w w . a m a z o n . c o m /d p /178588025X . If you'd like to join our team of regular reviewers, you can e-mail us at customerreviews@packtpub.com. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!
📄 Page 11
Table of Contents Preface 1 Chapter 1: Let us Make System Programming Great Again 7 Introduction 7 Setting up Rust in Linux and macOS 8 Getting ready 8 How to do it... 8 Uninstalling Rust 10 Rust's compiler version 10 Advanced installation options 11 Troubleshooting 11 How it works... 12 Setting up Rust in Windows 13 Getting ready 13 How to do it... 13 How it works... 13 Creating your first Rust program 13 Getting ready 13 How to do it... 14 How it works... 14 Defining a variable assignment 15 Getting ready 15 How to do it... 16 How it works... 17 Setting up Boolean and the character types 17 Getting ready 17 How to do it... 18 How it works... 18 Controlling decimal points, number formats, and named arguments 19 Getting ready 19 How to do it... 19 How it works... 20 Performing arithmetic operations 20 Getting ready 20 How to do it... 20 How it works... 21
📄 Page 12
[ ii ] Defining mutable variables 22 Getting ready 22 How to do it... 22 How it works... 23 Declaring and performing string operations 23 Getting ready 23 How to do it... 23 How it works... 26 Declaring arrays and using slices in Rust 28 Getting ready 28 How to do it... 28 How it works... 28 Declaring vectors in Rust 29 Getting ready 29 How to do it... 29 How it works... 31 Declaring tuples in Rust 31 Getting ready 31 How to do it... 31 How it works... 32 Performing calculations on two numbers 32 Getting ready 32 How to do it... 33 How it works... 34 Chapter 2: Advanced Programming with Rust 35 Introduction 35 Defining an expression 36 Getting ready 36 How to do it... 36 How it works... 37 Defining constants 37 Getting ready 37 How to do it... 38 How it works... 39 Performing variable bindings 39 Getting ready 39 How to do it... 39 How it works... 41 Performing type casting in Rust 41
📄 Page 13
[ iii ] Getting ready 41 How to do it... 41 How it works... 43 Decision-making with Rust 44 Getting ready 44 How to do it... 44 How it works... 45 Looping operations in Rust 46 Getting ready 46 How to do it... 46 How it works... 48 Defining the enum type 49 Getting ready 49 How to do it... 50 How it works... 51 Defining closures 52 Getting ready 52 How to do it... 52 How it works... 53 Performing pointer operations in Rust 53 Getting ready 53 How to do it... 54 How it works... 55 Defining your first user-defined data type 55 Getting ready 55 How to do it... 56 How it works... 57 Adding functionality to the user-defined data type 58 Getting ready 58 How to do it... 58 How it works... 59 Similar functionality for different data type 60 Getting ready 60 How to do it... 60 How it works... 62 Chapter 3: Deep Diving into Cargo 63 Introduction 63 Creating a new project using Cargo 63 Getting ready 64
📄 Page 14
[ iv ] How to do it... 64 How it works... 66 Downloading an external crate from crates.io 68 Getting ready 68 How to do it... 68 How it works... 71 Working on existing Cargo projects 72 Getting ready 73 How to do it... 73 How it works... 75 Running tests with Cargo 75 Getting ready 75 How to do it... 75 How it works... 76 Configuration management of the project 76 Getting ready 76 How to do it... 77 How it works... 78 Building the project on the Travis CI 79 Getting ready 79 How to do it... 80 How it works... 81 Uploading to crates.io 82 Getting ready 82 How to do it... 83 How it works... 83 Chapter 4: Creating Crates and Modules 85 Introduction 85 Defining a module in Rust 85 Getting ready 86 How to do it... 86 How it works... 88 Building a nested module 89 Getting ready 89 How to do it... 89 How it works... 91 Creating a module with struct 92 Getting ready 92 How to do it... 93
📄 Page 15
[ v ] How it works... 94 Controlling modules 97 Getting ready 97 How to do it... 97 How it works... 99 Accessing modules 99 Getting ready 99 How to do it... 100 How it works... 102 Creating a file hierarchy 103 Getting ready 104 How to do it... 104 How it works... 108 Building libraries in Rust 109 Getting ready 109 How to do it... 109 How it works... 110 Calling external crates 111 Getting ready 111 How to do it... 111 How it works... 112 Chapter 5: Deep Dive into Parallelism 113 Introduction 113 Creating a thread in Rust 114 Getting ready 114 How to do it... 114 How it works... 115 Spawning multiple threads 115 Getting ready 116 How to do it... 116 How it works... 117 Holding threads in a vector 118 Getting ready 118 How to do it... 118 How it works... 121 Sharing data between threads using channels 121 Getting ready 121 How to do it... 122 How it works... 123
📄 Page 16
[ vi ] Implementing safe mutable access 124 Getting ready 124 How to do it... 125 How it works... 126 Creating child processes 127 Getting ready 127 How to do it... 127 How it works... 128 Waiting for a child process 129 Getting ready 129 How to do it... 129 How it works... 130 Making sequential code parallel 131 Getting ready 131 How to do it... 133 How it works... 135 Chapter 6: Efficient Error Handling 136 Introduction 136 Implementing panic 137 Getting ready 137 How to do it... 137 How it works... 138 Implementing Option 138 Getting ready 139 How to do it... 139 How it works... 141 Creating map combinator 142 Getting ready 142 How to do it... 142 How it works... 144 Creating and_then combinator 145 Getting ready 145 How to do it... 146 How it works... 147 Creating map for the Result type 148 Getting ready 148 How to do it... 149 How it works... 150 Implementing aliases 151
📄 Page 17
[ vii ] Getting ready 151 How to do it... 151 How it works... 153 Handling multiple errors 153 Getting ready 153 How to do it... 154 How it works... 155 Implementing early returns 156 Getting ready 156 How to do it... 156 How it works... 157 Implementing the try! macro 158 Getting ready 158 How to do it... 158 How it works... 160 Defining your own error types 160 Getting ready 160 How to do it... 161 How it works... 163 Implementing the boxing of errors 164 Getting ready 164 How to do it... 164 How it works... 167 Chapter 7: Hacking Macros 169 Introduction 169 Building macros in Rust 170 Getting ready 170 How to do it... 170 How it works... 171 Implementing matching in macros 171 Getting ready 171 How to do it... 172 How it works... 172 Playing with common Rust macros 173 Getting ready 173 How to do it... 173 How it works... 174 Implementing designators 175 Getting ready 175
📄 Page 18
[ viii ] How to do it... 175 How it works... 176 Overloading macros 177 Getting ready 177 How to do it... 178 How it works... 179 Implementing repeat 179 Getting ready 179 How to do it... 180 How it works... 181 Implementing DRY 181 Getting ready 181 How to do it... 182 How it works... 184 Chapter 8: Integrating Rust with Other Languages 186 Introduction 186 Calling C operations from Rust 187 Getting ready 187 How to do it... 188 How it works... 189 Calling Rust commands from C 190 Getting ready 191 How to do it... 191 How it works... 193 Calling Rust operations from Node.js apps 194 Getting ready 195 How to do it... 195 How it works... 197 Calling Rust operations from Python 198 Getting ready 198 How to do it... 199 How it works... 201 Writing a Python module in Rust 201 Getting ready 202 How to do it... 203 How it works... 204 Chapter 9: Web Development with Rust 206 Introduction 206
📄 Page 19
[ ix ] Setting up a web server 207 Getting ready 207 How to do it... 209 How it works... 210 Creating endpoints 211 Getting ready 211 How to do it... 212 How it works... 214 Handling JSONRequests 215 Getting ready 215 How to do it... 216 How it works... 218 Building custom error handlers 219 Getting ready 219 How to do it... 220 How it works... 222 Hosting templates 223 Getting ready 224 How to do it... 225 How it works... 227 Chapter 10: Advanced Web Development in Rust 228 Introduction 228 Setting up the API 228 Getting ready 229 How to do it... 230 How it works... 233 Saving user data in MongoDB 234 Getting ready 234 How to do it... 236 Fetching user data 241 Getting ready 241 How to do it... 242 How it works... 247 Deleting user data 248 Getting ready 248 How to do it... 249 How it works... 254 Chapter 11: Advanced Rust Tools and Libraries 256
📄 Page 20
[ x ] Introduction 257 Setting up rustup 257 Getting ready 257 How to do it... 258 How it works... 260 Setting up rustfmt 260 Getting ready 260 How to do it... 262 How it works... 263 Setting up rust-clippy 264 Getting ready 264 How to do it... 265 How it works... 268 Setting up and testing with Servo 269 Getting ready 269 How to do it... 269 How it works... 270 Generating random numbers 270 Getting ready 270 How to do it... 271 How it works... 272 Writing lines to a file 272 Getting ready 272 How to do it... 273 How it works... 274 Parsing unstructured JSON 275 Getting ready 275 How to do it... 276 How it works... 277 Parsing URL from a string 278 Getting ready 278 How to do it... 279 How it works... 280 Decompressing a tarball 281 Getting ready 281 How to do it... 281 How it works... 283 Compressing a directory to a tarball 284 Getting ready 284
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now
Back to List