178 lines
12 KiB
HTML
178 lines
12 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<title>Introduction</title>
|
||
</head>
|
||
<body>
|
||
<h1 id="introduction"><a class="header" href="#introduction">Introduction</a></h1>
|
||
<section class="note" aria-role="note">
|
||
<p>Note: This edition of the book is the same as <a href="https://nostarch.com/rust-programming-language-3rd-edition">The Rust Programming
|
||
Language</a> available in print and ebook format from <a href="https://nostarch.com/">No Starch
|
||
Press</a>.</p>
|
||
</section>
|
||
<p>Welcome to <em>The Rust Programming Language</em>, an introductory book about Rust.
|
||
The Rust programming language helps you write faster, more reliable software.
|
||
High-level ergonomics and low-level control are often at odds in programming
|
||
language design; Rust challenges that conflict. Through balancing powerful
|
||
technical capacity and a great developer experience, Rust gives you the option
|
||
to control low-level details (such as memory usage) without all the hassle
|
||
traditionally associated with such control.</p>
|
||
<h2 id="who-rust-is-for"><a class="header" href="#who-rust-is-for">Who Rust Is For</a></h2>
|
||
<p>Rust is ideal for many people for a variety of reasons. Let’s look at a few of
|
||
the most important groups.</p>
|
||
<h3 id="teams-of-developers"><a class="header" href="#teams-of-developers">Teams of Developers</a></h3>
|
||
<p>Rust is proving to be a productive tool for collaborating among large teams of
|
||
developers with varying levels of systems programming knowledge. Low-level code
|
||
is prone to various subtle bugs, which in most other languages can only be
|
||
caught through extensive testing and careful code review by experienced
|
||
developers. In Rust, the compiler plays a gatekeeper role by refusing to
|
||
compile code with these elusive bugs, including concurrency bugs. By working
|
||
alongside the compiler, the team can spend its time focusing on the program’s
|
||
logic rather than chasing down bugs.</p>
|
||
<p>Rust also brings contemporary developer tools to the systems programming world:</p>
|
||
<ul>
|
||
<li>Cargo, the included dependency manager and build tool, makes adding,
|
||
compiling, and managing dependencies painless and consistent across the Rust
|
||
ecosystem.</li>
|
||
<li>The <code>rustfmt</code> formatting tool ensures a consistent coding style across
|
||
developers.</li>
|
||
<li>The Rust Language Server powers integrated development environment (IDE)
|
||
integration for code completion and inline error messages.</li>
|
||
</ul>
|
||
<p>By using these and other tools in the Rust ecosystem, developers can be
|
||
productive while writing systems-level code.</p>
|
||
<h3 id="students"><a class="header" href="#students">Students</a></h3>
|
||
<p>Rust is for students and those who are interested in learning about systems
|
||
concepts. Using Rust, many people have learned about topics like operating
|
||
systems development. The community is very welcoming and happy to answer
|
||
students’ questions. Through efforts such as this book, the Rust teams want to
|
||
make systems concepts more accessible to more people, especially those new to
|
||
programming.</p>
|
||
<h3 id="companies"><a class="header" href="#companies">Companies</a></h3>
|
||
<p>Hundreds of companies, large and small, use Rust in production for a variety of
|
||
tasks, including command line tools, web services, DevOps tooling, embedded
|
||
devices, audio and video analysis and transcoding, cryptocurrencies,
|
||
bioinformatics, search engines, Internet of Things applications, machine
|
||
learning, and even major parts of the Firefox web browser.</p>
|
||
<h3 id="open-source-developers"><a class="header" href="#open-source-developers">Open Source Developers</a></h3>
|
||
<p>Rust is for people who want to build the Rust programming language, community,
|
||
developer tools, and libraries. We’d love to have you contribute to the Rust
|
||
language.</p>
|
||
<h3 id="people-who-value-speed-and-stability"><a class="header" href="#people-who-value-speed-and-stability">People Who Value Speed and Stability</a></h3>
|
||
<p>Rust is for people who crave speed and stability in a language. By speed, we
|
||
mean both how quickly Rust code can run and the speed at which Rust lets you
|
||
write programs. The Rust compiler’s checks ensure stability through feature
|
||
additions and refactoring. This is in contrast to the brittle legacy code in
|
||
languages without these checks, which developers are often afraid to modify. By
|
||
striving for zero-cost abstractions—higher-level features that compile to
|
||
lower-level code as fast as code written manually—Rust endeavors to make safe
|
||
code be fast code as well.</p>
|
||
<p>The Rust language hopes to support many other users as well; those mentioned
|
||
here are merely some of the biggest stakeholders. Overall, Rust’s greatest
|
||
ambition is to eliminate the trade-offs that programmers have accepted for
|
||
decades by providing safety <em>and</em> productivity, speed <em>and</em> ergonomics. Give
|
||
Rust a try, and see if its choices work for you.</p>
|
||
<h2 id="who-this-book-is-for"><a class="header" href="#who-this-book-is-for">Who This Book Is For</a></h2>
|
||
<p>This book assumes that you’ve written code in another programming language, but
|
||
it doesn’t make any assumptions about which one. We’ve tried to make the
|
||
material broadly accessible to those from a wide variety of programming
|
||
backgrounds. We don’t spend a lot of time talking about what programming <em>is</em>
|
||
or how to think about it. If you’re entirely new to programming, you would be
|
||
better served by reading a book that specifically provides an introduction to
|
||
programming.</p>
|
||
<h2 id="how-to-use-this-book"><a class="header" href="#how-to-use-this-book">How to Use This Book</a></h2>
|
||
<p>In general, this book assumes that you’re reading it in sequence from front to
|
||
back. Later chapters build on concepts in earlier chapters, and earlier
|
||
chapters might not delve into details on a particular topic but will revisit
|
||
the topic in a later chapter.</p>
|
||
<p>You’ll find two kinds of chapters in this book: concept chapters and project
|
||
chapters. In concept chapters, you’ll learn about an aspect of Rust. In project
|
||
chapters, we’ll build small programs together, applying what you’ve learned so
|
||
far. Chapter 2, Chapter 12, and Chapter 21 are project chapters; the rest are
|
||
concept chapters.</p>
|
||
<p><strong>Chapter 1</strong> explains how to install Rust, how to write a “Hello, world!”
|
||
program, and how to use Cargo, Rust’s package manager and build tool. <strong>Chapter
|
||
2</strong> is a hands-on introduction to writing a program in Rust, having you build
|
||
up a number-guessing game. Here, we cover concepts at a high level, and later
|
||
chapters will provide additional detail. If you want to get your hands dirty
|
||
right away, Chapter 2 is the place for that. If you’re a particularly
|
||
meticulous learner who prefers to learn every detail before moving on to the
|
||
next, you might want to skip Chapter 2 and go straight to <strong>Chapter 3</strong>, which
|
||
covers Rust features that are similar to those of other programming languages;
|
||
then, you can return to Chapter 2 when you’d like to work on a project applying
|
||
the details you’ve learned.</p>
|
||
<p>In <strong>Chapter 4</strong>, you’ll learn about Rust’s ownership system. <strong>Chapter 5</strong>
|
||
discusses structs and methods. <strong>Chapter 6</strong> covers enums, <code>match</code> expressions,
|
||
and the <code>if let</code> and <code>let...else</code> control flow constructs. You’ll use structs
|
||
and enums to make custom types.</p>
|
||
<p>In <strong>Chapter 7</strong>, you’ll learn about Rust’s module system and about privacy
|
||
rules for organizing your code and its public application programming interface
|
||
(API). <strong>Chapter 8</strong> discusses some common collection data structures that the
|
||
standard library provides: vectors, strings, and hash maps. <strong>Chapter 9</strong>
|
||
explores Rust’s error-handling philosophy and techniques.</p>
|
||
<p><strong>Chapter 10</strong> digs into generics, traits, and lifetimes, which give you the
|
||
power to define code that applies to multiple types. <strong>Chapter 11</strong> is all
|
||
about testing, which even with Rust’s safety guarantees is necessary to ensure
|
||
that your program’s logic is correct. In <strong>Chapter 12</strong>, we’ll build our own
|
||
implementation of a subset of functionality from the <code>grep</code> command line tool
|
||
that searches for text within files. For this, we’ll use many of the concepts
|
||
we discussed in the previous chapters.</p>
|
||
<p><strong>Chapter 13</strong> explores closures and iterators: features of Rust that come from
|
||
functional programming languages. In <strong>Chapter 14</strong>, we’ll examine Cargo in
|
||
more depth and talk about best practices for sharing your libraries with
|
||
others. <strong>Chapter 15</strong> discusses smart pointers that the standard library
|
||
provides and the traits that enable their functionality.</p>
|
||
<p>In <strong>Chapter 16</strong>, we’ll walk through different models of concurrent
|
||
programming and talk about how Rust helps you program in multiple threads
|
||
fearlessly. In <strong>Chapter 17</strong>, we build on that by exploring Rust’s async and
|
||
await syntax, along with tasks, futures, and streams, and the lightweight
|
||
concurrency model they enable.</p>
|
||
<p><strong>Chapter 18</strong> looks at how Rust idioms compare to object-oriented programming
|
||
principles you might be familiar with. <strong>Chapter 19</strong> is a reference on
|
||
patterns and pattern matching, which are powerful ways of expressing ideas
|
||
throughout Rust programs. <strong>Chapter 20</strong> contains a smorgasbord of advanced
|
||
topics of interest, including unsafe Rust, macros, and more about lifetimes,
|
||
traits, types, functions, and closures.</p>
|
||
<p>In <strong>Chapter 21</strong>, we’ll complete a project in which we’ll implement a
|
||
low-level multithreaded web server!</p>
|
||
<p>Finally, some appendixes contain useful information about the language in a
|
||
more reference-like format. <strong>Appendix A</strong> covers Rust’s keywords, <strong>Appendix
|
||
B</strong> covers Rust’s operators and symbols, <strong>Appendix C</strong> covers derivable traits
|
||
provided by the standard library, <strong>Appendix D</strong> covers some useful development
|
||
tools, and <strong>Appendix E</strong> explains Rust editions. In <strong>Appendix F</strong>, you can
|
||
find translations of the book, and in <strong>Appendix G</strong> we’ll cover how Rust is
|
||
made and what nightly Rust is.</p>
|
||
<p>There is no wrong way to read this book: If you want to skip ahead, go for it!
|
||
You might have to jump back to earlier chapters if you experience any
|
||
confusion. But do whatever works for you.</p>
|
||
<p><span id="ferris"></span></p>
|
||
<p>An important part of the process of learning Rust is learning how to read the
|
||
error messages the compiler displays: These will guide you toward working code.
|
||
As such, we’ll provide many examples that don’t compile along with the error
|
||
message the compiler will show you in each situation. Know that if you enter
|
||
and run a random example, it may not compile! Make sure you read the
|
||
surrounding text to see whether the example you’re trying to run is meant to
|
||
error. In most situations, we’ll lead you to the correct version of any code
|
||
that doesn’t compile. Ferris will also help you distinguish code that isn’t
|
||
meant to work:</p>
|
||
<div class="table-wrapper">
|
||
<table>
|
||
<thead>
|
||
<tr><th>Ferris</th><th>Meaning</th></tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr><td></td><td>This code does not compile!</td></tr>
|
||
<tr><td></td><td>This code panics!</td></tr>
|
||
<tr><td></td><td>This code does not produce the desired behavior.</td></tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<p>In most situations, we’ll lead you to the correct version of any code that
|
||
doesn’t compile.</p>
|
||
<h2 id="source-code"><a class="header" href="#source-code">Source Code</a></h2>
|
||
<p>The source files from which this book is generated can be found on
|
||
<a href="https://github.com/rust-lang/book/tree/main/src">GitHub</a>.</p>
|
||
</body>
|
||
</html>
|