Rust Crash Course Build High-Performance, Efficient and Productive Software with the Power of Next-Generation Programming… (Abhishek Kumar) (Z-Library)

Author: Abhishek Kumar

商业

No Description

📄 File Format: PDF
💾 File Size: 3.2 MB
17
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
(This page has no text content)
📄 Page 3
Rust Crash Course Build High-Performance, Efficient, and Productive Software with the Power of Next-Generation Programming Skills Abhishek Kumar www.bpbonline.com
📄 Page 4
Copyright © 2022 BPB Online 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 BPB Online 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. BPB Online has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, BPB Online cannot guarantee the accuracy of this information. Group Product Manager: Marianne Conor Publishing Product Manager: Eva Brawn Senior Editor: Connell Content Development Editor: Melissa Monroe Technical Editor: Anne Stokes Copy Editor: Joe Austin Language Support Editor: Justin Baldwin Project Coordinator: Tyler Horan Proofreader: Khloe Styles Indexer: V. Krishnamurthy Production Designer: Malcolm D'Souza Marketing Coordinator: Kristen Kramer First published: July 2022 Published by BPB Online WeWork, 119 Marylebone Road London NW1 5PU UK | UAE | INDIA | SINGAPORE ISBN 978-93-5551-095-2 www.bpbonline.com
📄 Page 5
Dedicated to My Parents: Smt Usha Shri Ram Vijay Kumar & My wife Harshita and My siblings Avinash and Anjali
📄 Page 6
About the Author Abhishek Kumar has been involved in the design and development of complex enterprise-grade software for about 10 years. He has gained extensive systems programming experience while working at Adobe, Intel, ARM, Samsung, and Nvidia. He is currently working as a Deep Learning R&D Engineer and is passionate about teaching programming and machine learning. He is the creator of some of the successful courses on C++, Rust, Lua, Data Structures and algorithms, and Machine Learning. He holds a US patent relating to Computer Vision and Deep Learning. Abhishek Kumar holds Bachelor of Technology in Electrical from IIT Delhi and Master of Technology in Information and Communication Technology from IIT Delhi.
📄 Page 7
Acknowledgements I would like to thank my parents, my wife and my siblings for continued support and encouragement during writing of this book. I could never have completed this book without their support. I am also grateful to the active Rust community and some of the colleagues especially Mr. Reetesh for providing me with the knowledge and resources required in writing this book. I would also like to mention the team at BPB Publication (including Surbhi and Lubna) for their support and continuous feedback on different chapters of the book. Lastly, I would like to thank BPB publication for providing me with the opportunity to author this book.
📄 Page 8
Preface Rust is a powerful Systems programming language directed towards reliability, speed, memory safety, and parallelism. Rust enables you to write efficient programs that are blazingly fast (thanks to its zero-cost abstractions done at compile time) and free from memory bugs, concurrency bugs (data races) and undefined behavior. While Rust is great for low-level Systems Programming, it’s also being used for web apps, network services, and embedded programs. These features along with great tools, documentation and a welcoming community have made a favorite language for developers. The book is divided into four sections, each building on top of earlier sections. So, it is advisable to follow the sequence of chapters for better understanding. Chapter 1 explains Rust installation, hello world program, and using cargo (package manager and build tool). Chapter 2 covers the general programming concepts like variables and mutability, data types, comments, and control flow. We move to more advanced and core features of Rust in Chapter 3 where we learn about Ownership, borrowing and references. Chapter 4 discusses Structs, Enums and common collections in standard library. In Chapter 5 we will learn about Rust’s module system which helps you to manage your code’s organization. Chapter 6 explains error handling in Rust. Chapter 7 covers generics, traits, and lifetimes, enabling you to define code that applies to multiple types. Chapter 8 digs into writing automated tests. Chapter 9 explores functional programming features like closures and iterators. In Chapter 10, we’ll discuss smart pointers. In Chapter 11, we’ll study about concurrent programming and how Rust helps you to write multi- threaded program fearlessly. Chapter 12 examines object-oriented programming principles in Rust. In Chapter 13 we use the concepts learnt from earlier chapters to implement some of the popular data structures using Rust, like Linked List, Binary Trees, Hash Tables and Graph representations. In Chapters 14 and 15, we will explore the industry-wide acceptance of Rust as low-level Systems programming language. We’ll learn how to develop a Windows application using Windows APIs and an Android application with Rust. Chapter 16 to 19 are all about projects, where we’ll do projects for command-line, web, and embedded applications.
📄 Page 9
After reading this book, you will be skilled with the fundamentals of Rust programming and will be able to write idiomatic Rust code for your projects, write better tests and documentation. The details are listed below. Chapter 1 is all about getting started with Rust. It explains the process of installing Rust on Windows, MacOS and Linux operating systems, followed by a hello world program in Rust. The chapter also explains how to use cargo (package manager and build tool). Chapter 2 covers some of the general programming concepts which are present in almost all the programming languages. These concepts are not unique to Rust. The chapter explains concepts like variables, data types, functions, etc. Chapter 3 talks about Rust’s unique feature called Ownership, which enables Rust to guarantee memory safety without the need for a garbage collector. The chapter also discusses concepts like borrowing, references, and slices. Chapter 4 explains Structures or Struct, Enums and common Collections in the standard library. Structs enables you to group related values. Enums help you to define a type by enumerating its possible variants. Collections, unlike other data types contain multiple values. This chapter teaches you about arrays, tuples, vector, string, and hash map. Chapter 5 covers Rust’s module system which helps you to manage your code’s organization. It is important to group related functionality together. Rust provides several features like packages, crates, modules, and paths for this purpose. These features are also referred to as module system. Chapter 6 covers different types of errors and how to handle these. Errors are a part and parcel of software programs. Rust provides several features for handling error situations. Unlike many other programming languages, Rust groups errors into two categories – recoverable and unrecoverable errors. Chapter 7 explains about generics and traits, which are tools to handle duplication of code logic. Like a C++ template, a generic function or type can be used with values of many different types. Traits are like interfaces in other programming languages like Java or C#. This chapter explains how traits are used, how this work and how to define your own traits. Chapter 8 explains how to write a test case to validate whether a piece of code is functioning as expected. It explains the anatomy of a test function and different categories of tests – Unit Tests and Integration tests.
📄 Page 10
Chapter 9 teaches you about functional programming features in Rust – iterators and closures. In functional programming functions are treated as first-class citizens i.e., these can be assigned to variables, passed as arguments to functions and returned from functions. This chapter explains closures which are a function-like construct that can be stored in a variable and iterators which are a way of processing a series of elements. The chapter also compares the performance of iterators vs loops. Chapter 10 explains pointers and smart pointers in rust. Pointers in general refer to variables that store the address of another variable. Smart pointers are data structures that not only act like pointers, but also have additional capabilities like reference counting. The concept of smart pointer is not unique to Rust and is present in C++ and other languages as well. The chapter covers the most common smart pointers present in the library. Chapter 11 talks about safe and efficient concurrent programming in Rust. Concurrent programming and parallel programming are gaining importance as computers can take advantage of multiple processors. In concurrent programming different parts of a program execute independently, and in parallel programming different parts of a program execute simultaneously. Rust tries to simplify the handling of concurrency and parallelism as compared to other languages. Chapter 12 explores certain characteristics of object-oriented programming (OOP) and how these characteristics are supported in Rust. It also explains how to implement an object-oriented design pattern in Rust and trade-offs for implementing OOP solution vs solution using some of the Rust features. Chapter 13 explains how to implement some of the most common data structures like Linked List, Trees, Hash Tables and Graph representations using Rust. Chapter 14 teaches you how to use any Windows API using windows crate. The chapter uses the development of a mini calculator application to explain the workflow of application development in Rust using Windows APIs. Chapter 15 explains how Rust is gaining momentum and industry-wide acceptance, with specific case of inclusion to Android Open-Source Project (AOSP). The low-level components of Android OS development use Systems programming languages like C and C++. Rust provides similar low-level control and performance but adds memory protection. Considering this, the
📄 Page 11
AOSP now supports the Rust programming language for developing the OS itself. Chapter 16 builds a command line application called a To-do list using Rust. Command line applications are a great way to get started with learning Rust. Chapter 17 explains how to develop a web application for authenticating username and password using Rust. The application can be run in browser and will have basic UI components like username, password fields and a Login Button. The chapter uses a few key concepts like WebAssembly and how to include html into Rust code. Chapter 18 explains how to use Rust for embedded applications, by implementing a hello world application using QEMU, a popular open-source hardware emulator. The goal of the project is to understand the writing, building, and debugging embedded programs. Chapter 19 covers the basic functioning of a neural network and implements a simple Binary Classifier to classify an image into two classes. Deep learning has been gaining lots of momentum in the past decade, so this chapters intends to be a good starting point for Rust programmers who want to use Rust for deep learning tasks.
📄 Page 12
Code Bundle and Coloured Images Please follow the link to download the Code Bundle and the Coloured Images of the book: https://rebrand.ly/ecacb7 The code bundle for the book is also hosted on GitHub at https://github.com/bpbpublications/Rust-Crash-Course. In case there's an update to the code, it will be updated on the existing GitHub repository. We have code bundles from our rich catalogue of books and videos available at https://github.com/bpbpublications. Check them out! Errata We take immense pride in our work at BPB Publications and follow best practices to ensure the accuracy of our content to provide with an indulging reading experience to our subscribers. Our readers are our mirrors, and we use their inputs to reflect and improve upon human errors, if any, that may have occurred during the publishing processes involved. To let us maintain the quality and help us reach out to any readers who might be having difficulties due to any unforeseen errors, please write to us at : errata@bpbonline.com Your support, suggestions and feedbacks are highly appreciated by the BPB Publications’ Family. Did you know that BPB offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.bpbonline.com and as a print book customer,
📄 Page 13
you are entitled to a discount on the eBook copy. Get in touch with us at: business@bpbonline.com for more details. At www.bpbonline.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 BPB books and eBooks.
📄 Page 14
Piracy If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at business@bpbonline.com with a link to the material. If you are interested in becoming an author If there is a topic that you have expertise in, and you are interested in either writing or contributing to a book, please visit www.bpbonline.com. We have worked with thousands of developers and tech professionals, just like you, to help them share their insights 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. Reviews Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions. We at BPB can understand what you think about our products, and our authors can see your feedback on their book. Thank you! For more information about BPB, please visit www.bpbonline.com.
📄 Page 15
Table of Contents 1. Setup and Installation of Rust Structure Objectives Installing Rust Updating and uninstalling Writing a Hello World program Working with cargo Creating a Hello World project with cargo Understanding Cargo.toml file Building and running with cargo Conclusion Questions Points to remember 2. General Programming Concepts Structure Objectives Variables Declaring a variable Mutability Data types Scalar data types Integer Floating-point Boolean Characters Compound data types Tuples Arrays Adding comments Functions Control flow
📄 Page 16
if expression if … else if … else if Loops loop while loop for loop Conclusion Problems Points to remember 3. Ownership and Memory Management Introduction Structure Objectives Ownership principal in Rust Memory allocation on stack and heap String data type Memory allocation for String type Move Clone Copy References and borrowing Slice Conclusion Questions Points to remember 4. Structs, Enums, and Collections Introduction Structure Objectives Structs Defining structs Instantiating and using structs Field init shorthand Struct update syntax
📄 Page 17
Tuple structs Methods Defining methods Using methods Associated functions Enums Option enum The match operator Collections Vector Creating a vector Updating a vector Accessing elements of a vector String Creating a String Updating a String Accessing bytes and characters of a String Hash map Creating a hash map Accessing values in a hash map Removing a value from a hash map Conclusion Questions Points to remember 5. Organizing Your Code Introduction Structure Objectives Rust’s module system Packages Crates Creating a binary crate Creating a library crate Modules Paths and use Conclusion
📄 Page 18
Questions Points to remember 6. Error Handling Introduction Structure Objectives Rust’s error handling Recoverable errors and Result Common Result methods Unrecoverable errors and panic! Using Backtrace Changing the default panic behavior Conclusion Questions Points to remember 7. Generics and Traits Introduction Structure Objectives Generic data types in Rust Structs using generic types Functions using generic types Enums using generic types Methods using generic types Traits Defining a trait Implementing a trait Default implementation of Trait’s methods Implementing multiple traits Traits as function parameters impl trait syntax Trait bound syntax Multiple parameters of same trait Parameters implementing multiple traits Conclusion
📄 Page 19
Questions Points to remember 8. Testing Your Code Introduction Structure Objectives Writing software tests Unit tests Writing a test function Assert helper macros Running test functions Running specific tests Ignoring execution of tests Integration tests Creating integration tests Running integration tests Conclusion Questions Points to remember 9. Iterators and Closures Introduction Structure Objectives Closures Defining and calling a closure Type inference Closure as member of a struct Capturing environment Iterators The Iterator trait Defining a counter with Iterator trait iter(), into_iter(), and iter_mut() Iterators versus loops Conclusion Questions
📄 Page 20
Points to remember 10. Smart Pointers Introduction Structure Objectives Pointers in Rust Smart pointers in Rust Box<T> Defining recursive types with Box<T> Rc<T> RefCell<T> Conclusion Questions Points to remember 11. Concurrency Introduction Structure Objectives Threads Threading models One-to-one model Many-to-one model Many-to-many model Creating a thread - spawn Waiting for a thread - join Message-passing concurrency Shared-state concurrency Mutex<T> Arc<T> Sync and Send traits Conclusion Questions Points to remember 12. Object-Oriented Features
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