Beginning Rust (eBook)
XVII, 376 Seiten
Apress (Verlag)
978-1-4842-3468-6 (ISBN)
- Get started programming with Rust
- Understand heterogeneous data structures and data sequences
- Define functions, generic functions, structs, and more
- Work with closures, changeable strings, ranges and slices
- Use traits and learn about lifetimes
Learn to program with Rust in an easy, step-by-step manner on Unix, Linux shell, macOS and the Windows command line. As you read this book, you'll build on the knowledge you gained in previous chapters and see what Rust has to offer. Beginning Rust starts with the basics of Rust, including how to name objects, control execution flow, and handle primitive types. You'll see how to do arithmetic, allocate memory, use iterators, and handle input/output. Once you have mastered these core skills, you'll work on handling errors and using the object-oriented features of Rust to build robust Rust applications in no time.Only a basic knowledge of programming is required, preferably in C or C++. To understand this book, it's enough to know what integers and floating-point numbers are, and to distinguish identifiers from string literals.After reading this book, you'll be ready to build Rust applications. What You'll LearnGet started programming with RustUnderstand heterogeneous data structures and data sequencesDefine functions, generic functions, structs, and moreWork with closures, changeable strings, ranges and slicesUse traits and learn about lifetimes Who This Book Is For Those who are new to Rust and who have at least some prior experience with programming in general: some C/C++ is recommended particularly.
Carlo Milanesi is a professional software developer and expert who uses Rust. He has contributed to the Rust development community, and also has done web application development in Linux with PHP, JavaScript, Java, Ionic and Vaadin frameworks. Lastly, he has been in involved in these other technologies: GUI design, 2D and 3D rendering, testing automation, database access. Carlo's applications include CAM/CAM for the stone machining industry, lens cutting laboratory automation, and corporate-wide web applications.
Table of Contents 4
About the Author 13
About the Technical Reviewer 14
Preface 15
Chapter 1: Printing on the Terminal 16
How to Start 16
Hello, World! 17
Printing Combinations of Literal Strings 19
Printing Several Lines of Text 20
Printing Integer Numbers 21
Command-Line Script 22
Comments 23
Chapter 2: Doing Arithmetic 24
Adding Integer Numbers 24
Other Operations Between Integer Numbers 25
Floating-Point Arithmetic 26
Sequences of Statements 28
Breaking Literal Strings 29
Chapter 3: Naming Objects 31
Associating Names to Values 31
Mutable Variables 33
Not Mutated Mutable Variables 35
Uninitialized Variables 36
The Leading Underscore 37
Boolean Values 39
Boolean Expressions 41
Type Consistency in Assignments 42
Change of Type and of Mutability 43
Assignment Arithmetic Operators 44
Using the Functions of the Standard Library 45
Chapter 4: Controlling Execution Flow 47
Conditional Statements (if) 47
Conditional Expressions 50
Conditioned Loops (while) 52
Infinite Loops (loop) 54
Counting Loops (for) 54
Variables Scopes 56
Chapter 5: Using Data Sequences 60
Arrays 60
Mutable Arrays 63
Arrays of Specified Size 64
Multidimensional Arrays 65
Vectors 66
Other Operations on Vectors 69
Empty Arrays and Vectors 70
Debug Print 71
Copying Arrays and Vectors 72
Chapter 6: Using Primitive Types 74
Non-Decimal Numeric Bases 75
Underscore in Numeric Literals 76
The Exponential Notation 77
The Various Kinds of Signed Integer Numbers 78
Unsigned Integer Number Types 81
Target-Dependent Integer-Number Types 81
Type Inference 83
The Type Inference Algorithm 85
Floating-Point Numeric Types 86
Explicit Conversions 87
Type Suffixes of Numeric Literals 88
All the Numeric Types 89
Booleans and Characters 90
The Empty Tuple 92
Array and Vector Types 93
Constants 94
Discovering the Type of an Expression 94
Chapter 7: Enumerating Cases 96
Enumerations 96
The match Construct 98
Relational Operators and Enums 100
Handling All the Cases 101
Using match with Numbers 102
Enumerations with Data 103
“match” Expressions 107
Use of Guards in match Constructs 107
Chapter 8: Using Heterogeneous Data Structures 109
The Tuples 109
The Structs 111
The Tuple-Structs 114
Lexical Conventions 115
Chapter 9: Defining Functions 117
Defining and Invoking a Function 117
Functions Defined After Their Use 118
Functions Shadowing Other Functions 119
Passing Arguments to a Function 120
Passing Arguments by Value 122
Returning a Value from a Function 123
Early Exit 124
Returning Several Values 126
How to Change a Variable of the Caller 127
Passing Arguments by Reference 128
Using References 130
Mutability of References 131
Chapter 10: Defining Generic Functions and Structs 133
Need of Generic Functions 133
Defining and Using Generic Functions 134
Inferring the Parametric Types 136
Defining and Using Generic Structs 137
Genericity Mechanics 138
Generic Arrays and Vectors 141
Generic Enums 141
Error Handling 143
Enum Standard Utility Functions 145
Chapter 11: Allocating Memory 147
The Various Kinds of Allocation 147
Linear Addressing 148
Static Allocation 150
Stack Allocation 151
Limitations of Stack Allocation 157
Heap Allocation 160
Heap Management 161
The Behavior of Box 162
Similarity with C and C++ 163
Boxing and Unboxing 164
Register Allocation 165
Chapter 12: Data Implementation 167
Discovering the Size of Objects 167
The use Directive 168
The Sizes of the Primitive Types 169
The Representation of Primitive Types 170
Location of Bytes in Memory 171
Sizes of Composite Data Types 173
Vector Allocation 174
Chapter 13: Defining Closures 177
The Need for “Disposable” Functions 177
Capturing the Environment 179
Closures 180
Other Examples 182
Chapter 14: Using Changeable Strings 184
Static Strings 184
Dynamic Strings 187
Implementation of String 188
Creating Strings 190
Concatenating Strings 191
Chapter 15: Ranges and Slices 194
The Ranges 194
Passing a Sequence to a Function 196
The Slices 198
Slicing 201
Out-of-Range Slicing 202
Mutable Slicing 203
Open-Ended Ranges and Slicing 204
Chapter 16: Using Iterators 207
String Characters 207
Scanning a String 209
Using Iterators in for Loops 211
Iterations Without Mutation 214
Iterations with Mutation 215
An Iterator Adapter: filter 219
The map Iterator Adapter 220
The enumerate Iterator Adapter 221
An Iterator Consumer: any 222
The all Iterator Consumer 224
The count Iterator Consumer 225
The sum Iterator Consumer 225
The min and max Iterator Consumers 226
The collect Consumer 227
Iterator Chains 229
Iterators Are “Lazy” 230
Chapter 17: Input/Output and Error Handling 232
Command-Line Arguments 232
Process Return Code 233
Environment Variables 234
Reading from the Console 235
Proper Runtime Error Handling 237
Writing to the Console 240
Converting a Value to a String 241
File Input/Output 242
Processing Text Files 244
Chapter 18: Using Traits 247
The Need for Traits 247
Traits to the Rescue 250
Generic Functions with No Trait Bounds 252
Scope of Traits 254
Traits with Multiple Functions 255
Methods 258
The “self” and “Self” Keywords 260
Standard Traits 262
The “Iterator” Trait 263
The “type” Keyword 266
Generic Traits 267
Using Associated Types to Simplify Generic Traits Use 272
The “Iterator” Standard Trait Declaration 275
Using Generic Iterators 278
Chapter 19: Object-Oriented Programming 280
Inherent Implementations 280
Peculiarities of Rust Object-Orientation 284
Mutating Methods 288
Constructors 289
Composition Instead of Inheritance 290
Memory Usage of Composition 293
Static Dispatch 293
Dynamic Dispatch 297
Implementation of References to Traits 299
Static vs. Dynamic Dispatch 300
Chapter 20: Standard Library Collections 302
Collections 302
Measuring Execution Time 303
Performing Arbitrary Insertions and Removals 304
Queues 307
Linked Lists 310
Binary Heaps 311
Ordered Sets and Unordered Sets 313
Ordered Dictionaries and Unordered Dictionaries 316
Collections in C++ and in Rust 318
Chapter 21: Drops, Moves, and Copies 319
Deterministic Destruction 319
Ownership 322
Destructors 323
Assignment Semantics 328
Copying vs. Moving Performance 330
Moving and Destroying Objects 333
Need for Copy Semantics 336
Cloning Objects 337
Making Types Cloneable or Copyable 339
Chapter 22: Borrowing and Lifetimes 342
Ownership and Borrowing 342
Object Lifetimes 343
Errors Regarding Borrowing 345
How to Prevent “Use After Drop” Errors 348
How to Prevent “Use After Change by an Alias” Errors 349
Listing the Possible Cases of Multiple Borrowings 350
Using a Block to Restrict Borrowing Scope 353
The Need of Lifetime Specifiers for Returned References 354
Usage and Meaning of Lifetime Specifiers 357
Checking the Validity of Lifetime Specifiers 360
Using the Lifetime Specifiers of Invoked Functions 363
Chapter 23: More About Lifetimes 366
Lifetime Elision 366
Lifetime Elision with Object-Oriented Programming 368
The Need of Lifetime Specifiers for Structs 368
Possible Lifetime Specifiers for Structs 371
Other Uses of Lifetime Specifiers 373
Index 376
Erscheint lt. Verlag | 22.3.2018 |
---|---|
Zusatzinfo | XVII, 376 p. 3 illus. |
Verlagsort | Berkeley |
Sprache | englisch |
Themenwelt | Mathematik / Informatik ► Informatik ► Programmiersprachen / -werkzeuge |
Schlagworte | Beginner • Beginning • C • C++ • Code • language • learn • Memory • Open Source • Performance • programming • Rust • source • Step by Step • Tutorial |
ISBN-10 | 1-4842-3468-5 / 1484234685 |
ISBN-13 | 978-1-4842-3468-6 / 9781484234686 |
Haben Sie eine Frage zum Produkt? |
Größe: 3,3 MB
DRM: Digitales Wasserzeichen
Dieses eBook enthält ein digitales Wasserzeichen und ist damit für Sie personalisiert. Bei einer missbräuchlichen Weitergabe des eBooks an Dritte ist eine Rückverfolgung an die Quelle möglich.
Dateiformat: PDF (Portable Document Format)
Mit einem festen Seitenlayout eignet sich die PDF besonders für Fachbücher mit Spalten, Tabellen und Abbildungen. Eine PDF kann auf fast allen Geräten angezeigt werden, ist aber für kleine Displays (Smartphone, eReader) nur eingeschränkt geeignet.
Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen dafür einen PDF-Viewer - z.B. den Adobe Reader oder Adobe Digital Editions.
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen dafür einen PDF-Viewer - z.B. die kostenlose Adobe Digital Editions-App.
Zusätzliches Feature: Online Lesen
Dieses eBook können Sie zusätzlich zum Download auch online im Webbrowser lesen.
Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.
aus dem Bereich