]>
Commit | Line | Data |
---|---|---|
1 | # Introduction | |
2 | ||
3 | > Note: This edition of the book is the same as [The Rust Programming | |
4 | > Language][nsprust] available in print and ebook format from [No Starch | |
5 | > Press][nsp]. | |
6 | ||
7 | [nsprust]: https://nostarch.com/rust | |
8 | [nsp]: https://nostarch.com/ | |
9 | ||
10 | Welcome to *The Rust Programming Language*, an introductory book about Rust. | |
11 | The Rust programming language helps you write faster, more reliable software. | |
12 | High-level ergonomics and low-level control are often at odds in programming | |
13 | language design; Rust challenges that conflict. Through balancing powerful | |
14 | technical capacity and a great developer experience, Rust gives you the option | |
15 | to control low-level details (such as memory usage) without all the hassle | |
16 | traditionally associated with such control. | |
17 | ||
18 | ## Who Rust Is For | |
19 | ||
20 | Rust is ideal for many people for a variety of reasons. Let’s look at a few of | |
21 | the most important groups. | |
22 | ||
23 | ### Teams of Developers | |
24 | ||
25 | Rust is proving to be a productive tool for collaborating among large teams of | |
26 | developers with varying levels of systems programming knowledge. Low-level code | |
27 | is prone to a variety of subtle bugs, which in most other languages can be | |
28 | caught only through extensive testing and careful code review by experienced | |
29 | developers. In Rust, the compiler plays a gatekeeper role by refusing to | |
30 | compile code with these elusive bugs, including concurrency bugs. By working | |
31 | alongside the compiler, the team can spend their time focusing on the program’s | |
32 | logic rather than chasing down bugs. | |
33 | ||
34 | Rust also brings contemporary developer tools to the systems programming world: | |
35 | ||
36 | * Cargo, the included dependency manager and build tool, makes adding, | |
37 | compiling, and managing dependencies painless and consistent across the Rust | |
38 | ecosystem. | |
39 | * Rustfmt ensures a consistent coding style across developers. | |
40 | * The Rust Language Server powers Integrated Development Environment (IDE) | |
41 | integration for code completion and inline error messages. | |
42 | ||
43 | By using these and other tools in the Rust ecosystem, developers can be | |
44 | productive while writing systems-level code. | |
45 | ||
46 | ### Students | |
47 | ||
48 | Rust is for students and those who are interested in learning about systems | |
49 | concepts. Using Rust, many people have learned about topics like operating | |
50 | systems development. The community is very welcoming and happy to answer | |
51 | student questions. Through efforts such as this book, the Rust teams want to | |
52 | make systems concepts more accessible to more people, especially those new to | |
53 | programming. | |
54 | ||
55 | ### Companies | |
56 | ||
57 | Hundreds of companies, large and small, use Rust in production for a variety of | |
58 | tasks. Those tasks include command line tools, web services, DevOps tooling, | |
59 | embedded devices, audio and video analysis and transcoding, cryptocurrencies, | |
60 | bioinformatics, search engines, Internet of Things applications, machine | |
61 | learning, and even major parts of the Firefox web browser. | |
62 | ||
63 | ### Open Source Developers | |
64 | ||
65 | Rust is for people who want to build the Rust programming language, community, | |
66 | developer tools, and libraries. We’d love to have you contribute to the Rust | |
67 | language. | |
68 | ||
69 | ### People Who Value Speed and Stability | |
70 | ||
71 | Rust is for people who crave speed and stability in a language. By speed, we | |
72 | mean the speed of the programs that you can create with Rust and the speed at | |
73 | which Rust lets you write them. The Rust compiler’s checks ensure stability | |
74 | through feature additions and refactoring. This is in contrast to the brittle | |
75 | legacy code in languages without these checks, which developers are often | |
76 | afraid to modify. By striving for zero-cost abstractions, higher-level features | |
77 | that compile to lower-level code as fast as code written manually, Rust | |
78 | endeavors to make safe code be fast code as well. | |
79 | ||
80 | The Rust language hopes to support many other users as well; those mentioned | |
81 | here are merely some of the biggest stakeholders. Overall, Rust’s greatest | |
82 | ambition is to eliminate the trade-offs that programmers have accepted for | |
83 | decades by providing safety *and* productivity, speed *and* ergonomics. Give | |
84 | Rust a try and see if its choices work for you. | |
85 | ||
86 | ## Who This Book Is For | |
87 | ||
88 | This book assumes that you’ve written code in another programming language but | |
89 | doesn’t make any assumptions about which one. We’ve tried to make the material | |
90 | broadly accessible to those from a wide variety of programming backgrounds. We | |
91 | don’t spend a lot of time talking about what programming *is* or how to think | |
92 | about it. If you’re entirely new to programming, you would be better served by | |
93 | reading a book that specifically provides an introduction to programming. | |
94 | ||
95 | ## How to Use This Book | |
96 | ||
97 | In general, this book assumes that you’re reading it in sequence from front to | |
98 | back. Later chapters build on concepts in earlier chapters, and earlier | |
99 | chapters might not delve into details on a topic; we typically revisit the | |
100 | topic in a later chapter. | |
101 | ||
102 | You’ll find two kinds of chapters in this book: concept chapters and project | |
103 | chapters. In concept chapters, you’ll learn about an aspect of Rust. In project | |
104 | chapters, we’ll build small programs together, applying what you’ve learned so | |
105 | far. Chapters 2, 12, and 20 are project chapters; the rest are concept chapters. | |
106 | ||
107 | Chapter 1 explains how to install Rust, how to write a Hello, world! program, | |
108 | and how to use Cargo, Rust’s package manager and build tool. Chapter 2 is a | |
109 | hands-on introduction to the Rust language. Here we cover concepts at a high | |
110 | level, and later chapters will provide additional detail. If you want to get | |
111 | your hands dirty right away, Chapter 2 is the place for that. At first, you | |
112 | might even want to skip Chapter 3, which covers Rust features similar to those | |
113 | of other programming languages, and head straight to Chapter 4 to learn about | |
114 | Rust’s ownership system. However, if you’re a particularly meticulous learner | |
115 | who prefers to learn every detail before moving on to the next, you might want | |
116 | to skip Chapter 2 and go straight to Chapter 3, returning to Chapter 2 when | |
117 | you’d like to work on a project applying the details you’ve learned. | |
118 | ||
119 | Chapter 5 discusses structs and methods, and Chapter 6 covers enums, `match` | |
120 | expressions, and the `if let` control flow construct. You’ll use structs and | |
121 | enums to make custom types in Rust. | |
122 | ||
123 | In Chapter 7, you’ll learn about Rust’s module system and about privacy rules | |
124 | for organizing your code and its public Application Programming Interface | |
125 | (API). Chapter 8 discusses some common collection data structures that the | |
126 | standard library provides, such as vectors, strings, and hash maps. Chapter 9 | |
127 | explores Rust’s error-handling philosophy and techniques. | |
128 | ||
129 | Chapter 10 digs into generics, traits, and lifetimes, which give you the power | |
130 | to define code that applies to multiple types. Chapter 11 is all about testing, | |
131 | which even with Rust’s safety guarantees is necessary to ensure your program’s | |
132 | logic is correct. In Chapter 12, we’ll build our own implementation of a subset | |
133 | of functionality from the `grep` command line tool that searches for text | |
134 | within files. For this, we’ll use many of the concepts we discussed in the | |
135 | previous chapters. | |
136 | ||
137 | Chapter 13 explores closures and iterators: features of Rust that come from | |
138 | functional programming languages. In Chapter 14, we’ll examine Cargo in more | |
139 | depth and talk about best practices for sharing your libraries with others. | |
140 | Chapter 15 discusses smart pointers that the standard library provides and the | |
141 | traits that enable their functionality. | |
142 | ||
143 | In Chapter 16, we’ll walk through different models of concurrent programming | |
144 | and talk about how Rust helps you to program in multiple threads fearlessly. | |
145 | Chapter 17 looks at how Rust idioms compare to object-oriented programming | |
146 | principles you might be familiar with. | |
147 | ||
148 | Chapter 18 is a reference on patterns and pattern matching, which are powerful | |
149 | ways of expressing ideas throughout Rust programs. Chapter 19 contains a | |
150 | smorgasbord of advanced topics of interest, including unsafe Rust and more | |
151 | about lifetimes, traits, types, functions, and closures. | |
152 | ||
153 | In Chapter 20, we’ll complete a project in which we’ll implement a low-level | |
154 | multithreaded web server! | |
155 | ||
156 | Finally, some appendixes contain useful information about the language in a | |
157 | more reference-like format. Appendix A covers Rust’s keywords, Appendix B | |
158 | covers Rust’s operators and symbols, Appendix C covers derivable traits | |
159 | provided by the standard library, and Appendix D covers macros. | |
160 | ||
161 | There is no wrong way to read this book: if you want to skip ahead, go for it! | |
162 | You might have to jump back to earlier chapters if you experience any | |
163 | confusion. But do whatever works for you. | |
164 | ||
165 | An important part of the process of learning Rust is learning how to read the | |
166 | error messages the compiler displays: these will guide you toward working code. | |
167 | As such, we’ll provide many examples of code that doesn’t compile along with | |
168 | the error message the compiler will show you in each situation. Know that if | |
169 | you enter and run a random example, it may not compile! Make sure you read the | |
170 | surrounding text to see whether the example you’re trying to run is meant to | |
171 | error. In most situations, we’ll lead you to the correct version of any code | |
172 | that doesn’t compile. | |
173 | ||
174 | ## Source Code | |
175 | ||
176 | The source files from which this book is generated can be found on | |
177 | [GitHub][book]. | |
178 | ||
179 | [book]: https://github.com/rust-lang/book/tree/master/second-edition/src |