]>
Commit | Line | Data |
---|---|---|
13cf67c4 XL |
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 | |
2b03887a FG |
27 | is prone to various subtle bugs, which in most other languages can be caught |
28 | only through extensive testing and careful code review by experienced | |
13cf67c4 XL |
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. | |
2b03887a FG |
39 | * The Rustfmt formatting tool ensures a consistent coding style across |
40 | developers. | |
13cf67c4 XL |
41 | * The Rust Language Server powers Integrated Development Environment (IDE) |
42 | integration for code completion and inline error messages. | |
43 | ||
44 | By using these and other tools in the Rust ecosystem, developers can be | |
45 | productive while writing systems-level code. | |
46 | ||
47 | ### Students | |
48 | ||
49 | Rust is for students and those who are interested in learning about systems | |
50 | concepts. Using Rust, many people have learned about topics like operating | |
51 | systems development. The community is very welcoming and happy to answer | |
52 | student questions. Through efforts such as this book, the Rust teams want to | |
53 | make systems concepts more accessible to more people, especially those new to | |
54 | programming. | |
55 | ||
56 | ### Companies | |
57 | ||
58 | Hundreds of companies, large and small, use Rust in production for a variety of | |
2b03887a FG |
59 | tasks, including command line tools, web services, DevOps tooling, embedded |
60 | devices, audio and video analysis and transcoding, cryptocurrencies, | |
13cf67c4 XL |
61 | bioinformatics, search engines, Internet of Things applications, machine |
62 | learning, and even major parts of the Firefox web browser. | |
63 | ||
64 | ### Open Source Developers | |
65 | ||
66 | Rust is for people who want to build the Rust programming language, community, | |
67 | developer tools, and libraries. We’d love to have you contribute to the Rust | |
68 | language. | |
69 | ||
70 | ### People Who Value Speed and Stability | |
71 | ||
72 | Rust is for people who crave speed and stability in a language. By speed, we | |
2b03887a FG |
73 | mean both how quickly Rust code can run and the speed at which Rust lets you |
74 | write programs. The Rust compiler’s checks ensure stability through feature | |
75 | additions and refactoring. This is in contrast to the brittle legacy code in | |
76 | languages without these checks, which developers are often afraid to modify. By | |
77 | striving for zero-cost abstractions, higher-level features that compile to | |
78 | lower-level code as fast as code written manually, Rust endeavors to make safe | |
79 | code be fast code as well. | |
13cf67c4 XL |
80 | |
81 | The Rust language hopes to support many other users as well; those mentioned | |
82 | here are merely some of the biggest stakeholders. Overall, Rust’s greatest | |
83 | ambition is to eliminate the trade-offs that programmers have accepted for | |
84 | decades by providing safety *and* productivity, speed *and* ergonomics. Give | |
85 | Rust a try and see if its choices work for you. | |
86 | ||
87 | ## Who This Book Is For | |
88 | ||
89 | This book assumes that you’ve written code in another programming language but | |
90 | doesn’t make any assumptions about which one. We’ve tried to make the material | |
91 | broadly accessible to those from a wide variety of programming backgrounds. We | |
92 | don’t spend a lot of time talking about what programming *is* or how to think | |
93 | about it. If you’re entirely new to programming, you would be better served by | |
94 | reading a book that specifically provides an introduction to programming. | |
95 | ||
96 | ## How to Use This Book | |
97 | ||
98 | In general, this book assumes that you’re reading it in sequence from front to | |
99 | back. Later chapters build on concepts in earlier chapters, and earlier | |
2b03887a FG |
100 | chapters might not delve into details on a particular topic but will revisit |
101 | the topic in a later chapter. | |
13cf67c4 XL |
102 | |
103 | You’ll find two kinds of chapters in this book: concept chapters and project | |
104 | chapters. In concept chapters, you’ll learn about an aspect of Rust. In project | |
105 | chapters, we’ll build small programs together, applying what you’ve learned so | |
106 | far. Chapters 2, 12, and 20 are project chapters; the rest are concept chapters. | |
107 | ||
e74abb32 | 108 | Chapter 1 explains how to install Rust, how to write a “Hello, world!” program, |
13cf67c4 | 109 | and how to use Cargo, Rust’s package manager and build tool. Chapter 2 is a |
2b03887a FG |
110 | hands-on introduction to writing a program in Rust, having you build up a |
111 | number guessing game. Here we cover concepts at a high level, and later | |
112 | chapters will provide additional detail. If you want to get your hands dirty | |
113 | right away, Chapter 2 is the place for that. Chapter 3 covers Rust features | |
114 | that are similar to those of other programming languages, and in Chapter 4 | |
115 | you’ll learn about Rust’s ownership system. If you’re a particularly meticulous | |
116 | learner who prefers to learn every detail before moving on to the next, you | |
117 | might want to skip Chapter 2 and go straight to Chapter 3, returning to Chapter | |
118 | 2 when you’d like to work on a project applying the details you’ve learned. | |
13cf67c4 XL |
119 | |
120 | Chapter 5 discusses structs and methods, and Chapter 6 covers enums, `match` | |
121 | expressions, and the `if let` control flow construct. You’ll use structs and | |
122 | enums to make custom types in Rust. | |
123 | ||
124 | In Chapter 7, you’ll learn about Rust’s module system and about privacy rules | |
125 | for organizing your code and its public Application Programming Interface | |
126 | (API). Chapter 8 discusses some common collection data structures that the | |
127 | standard library provides, such as vectors, strings, and hash maps. Chapter 9 | |
128 | explores Rust’s error-handling philosophy and techniques. | |
129 | ||
130 | Chapter 10 digs into generics, traits, and lifetimes, which give you the power | |
131 | to define code that applies to multiple types. Chapter 11 is all about testing, | |
132 | which even with Rust’s safety guarantees is necessary to ensure your program’s | |
133 | logic is correct. In Chapter 12, we’ll build our own implementation of a subset | |
134 | of functionality from the `grep` command line tool that searches for text | |
135 | within files. For this, we’ll use many of the concepts we discussed in the | |
136 | previous chapters. | |
137 | ||
138 | Chapter 13 explores closures and iterators: features of Rust that come from | |
139 | functional programming languages. In Chapter 14, we’ll examine Cargo in more | |
140 | depth and talk about best practices for sharing your libraries with others. | |
141 | Chapter 15 discusses smart pointers that the standard library provides and the | |
142 | traits that enable their functionality. | |
143 | ||
144 | In Chapter 16, we’ll walk through different models of concurrent programming | |
145 | and talk about how Rust helps you to program in multiple threads fearlessly. | |
146 | Chapter 17 looks at how Rust idioms compare to object-oriented programming | |
147 | principles you might be familiar with. | |
148 | ||
149 | Chapter 18 is a reference on patterns and pattern matching, which are powerful | |
150 | ways of expressing ideas throughout Rust programs. Chapter 19 contains a | |
151 | smorgasbord of advanced topics of interest, including unsafe Rust, macros, and | |
152 | more about lifetimes, traits, types, functions, and closures. | |
153 | ||
154 | In Chapter 20, we’ll complete a project in which we’ll implement a low-level | |
155 | multithreaded web server! | |
156 | ||
fc512014 | 157 | Finally, some appendices contain useful information about the language in a |
13cf67c4 XL |
158 | more reference-like format. Appendix A covers Rust’s keywords, Appendix B |
159 | covers Rust’s operators and symbols, Appendix C covers derivable traits | |
160 | provided by the standard library, Appendix D covers some useful development | |
064997fb FG |
161 | tools, and Appendix E explains Rust editions. In Appendix F, you can find |
162 | translations of the book, and in Appendix G we’ll cover how Rust is made and | |
163 | what nightly Rust is. | |
13cf67c4 XL |
164 | |
165 | There is no wrong way to read this book: if you want to skip ahead, go for it! | |
166 | You might have to jump back to earlier chapters if you experience any | |
167 | confusion. But do whatever works for you. | |
168 | ||
169 | <span id="ferris"></span> | |
170 | ||
171 | An important part of the process of learning Rust is learning how to read the | |
172 | error messages the compiler displays: these will guide you toward working code. | |
9fa01778 XL |
173 | As such, we’ll provide many examples that don’t compile along with the error |
174 | message the compiler will show you in each situation. Know that if you enter | |
175 | and run a random example, it may not compile! Make sure you read the | |
13cf67c4 XL |
176 | surrounding text to see whether the example you’re trying to run is meant to |
177 | error. Ferris will also help you distinguish code that isn’t meant to work: | |
178 | ||
136023e0 XL |
179 | | Ferris | Meaning | |
180 | |------------------------------------------------------------------------------------------------------------------|--------------------------------------------------| | |
181 | | <img src="img/ferris/does_not_compile.svg" class="ferris-explain" alt="Ferris with a question mark"/> | This code does not compile! | | |
182 | | <img src="img/ferris/panics.svg" class="ferris-explain" alt="Ferris throwing up their hands"/> | This code panics! | | |
183 | | <img src="img/ferris/not_desired_behavior.svg" class="ferris-explain" alt="Ferris with one claw up, shrugging"/> | This code does not produce the desired behavior. | | |
13cf67c4 XL |
184 | |
185 | In most situations, we’ll lead you to the correct version of any code that | |
186 | doesn’t compile. | |
187 | ||
188 | ## Source Code | |
189 | ||
190 | The source files from which this book is generated can be found on | |
191 | [GitHub][book]. | |
192 | ||
a2a8927a | 193 | [book]: https://github.com/rust-lang/book/tree/main/src |