|
2 | 2 |
|
3 | 3 | # Introduction
|
4 | 4 |
|
5 |
| -## Scope |
6 |
| - |
7 |
| -This is a tutorial for the Rust programming language. It assumes the |
8 |
| -reader is familiar with the basic concepts of programming, and has |
9 |
| -programmed in one or more other languages before. It will often make |
10 |
| -comparisons to other languages in the C family. This tutorial covers |
11 |
| -the fundamentals of the language, including the syntax, the type |
12 |
| -system and memory model, and generics. |
13 |
| -[Additional tutorials](#what-next) cover specific language features in |
14 |
| -greater depth. |
15 |
| - |
16 |
| -## Language overview |
17 |
| - |
18 |
| -Rust is a systems programming language with a focus on type safety, |
19 |
| -memory safety, concurrency and performance. It is intended for writing |
20 |
| -large, high-performance applications while preventing several classes |
21 |
| -of errors commonly found in languages like C++. Rust has a |
22 |
| -sophisticated memory model that makes possible many of the efficient |
23 |
| -data structures used in C++, while disallowing invalid memory accesses |
24 |
| -that would otherwise cause segmentation faults. Like other systems |
25 |
| -languages, it is statically typed and compiled ahead of time. |
| 5 | +Rust is a programming language with a focus on type safety, memory |
| 6 | +safety, concurrency and performance. It is intended for writing |
| 7 | +large-scale, high-performance software while preventing several |
| 8 | +classes of common errors. Rust has a sophisticated memory model that |
| 9 | +encourages efficient data structures and safe concurrency patterns, |
| 10 | +forbidding invalid memory accesses that would otherwise cause |
| 11 | +segmentation faults. It is statically typed and compiled ahead of |
| 12 | +time. |
26 | 13 |
|
27 | 14 | As a multi-paradigm language, Rust supports writing code in
|
28 |
| -procedural, functional and object-oriented styles. Some of its nice |
29 |
| -high-level features include: |
| 15 | +procedural, functional and object-oriented styles. Some of its |
| 16 | +pleasant high-level features include: |
30 | 17 |
|
31 |
| -* **Pattern matching and algebraic data types (enums).** Common in |
32 |
| - functional languages, pattern matching on ADTs provides a compact |
33 |
| - and expressive way to encode program logic. |
| 18 | +* **Pattern matching and algebraic data types (enums).** As |
| 19 | + popularized by functional languages, pattern matching on ADTs |
| 20 | + provides a compact and expressive way to encode program logic. |
| 21 | +* **Type inference.** Type annotations on local variable |
| 22 | + declarations are optional. |
34 | 23 | * **Task-based concurrency.** Rust uses lightweight tasks that do
|
35 | 24 | not share memory.
|
36 |
| -* **Higher-order functions.** Rust functions may take closures as |
37 |
| - arguments or return closures as return values. Closures in Rust are |
38 |
| - very powerful and used pervasively. |
39 |
| -* **Trait polymorphism.** Rust's type system features a unique |
40 |
| - combination of Java-style interfaces and Haskell-style typeclasses |
41 |
| - called _traits_. |
| 25 | +* **Higher-order functions.** Rust's efficient and flexible closures |
| 26 | + are heavily relied on to provide iteration and other control |
| 27 | + structures |
42 | 28 | * **Parametric polymorphism (generics).** Functions and types can be
|
43 |
| - parameterized over type variables with optional type constraints. |
44 |
| -* **Type inference.** Type annotations on local variable |
45 |
| - declarations can be omitted. |
46 |
| - |
47 |
| -## First impressions |
48 |
| - |
49 |
| -As a curly-brace language in the tradition of C, C++, and JavaScript, |
50 |
| -Rust looks a lot like other languages you may be familiar with. |
| 29 | + parameterized over type variables with optional trait-based type |
| 30 | + constraints. |
| 31 | +* **Trait polymorphism.** Rust's type system features a unique |
| 32 | + combination of type classes and object-oriented interfaces. |
51 | 33 |
|
52 |
| -~~~~ |
53 |
| -fn boring_old_factorial(n: int) -> int { |
54 |
| - let mut result = 1, i = 1; |
55 |
| - while i <= n { |
56 |
| - result *= i; |
57 |
| - i += 1; |
58 |
| - } |
59 |
| - return result; |
60 |
| -} |
61 |
| -~~~~ |
| 34 | +## Scope |
62 | 35 |
|
63 |
| -Several differences from C stand out. Types do not come before, but |
64 |
| -after variable names (preceded by a colon). For local variables |
65 |
| -(introduced with `let`), types are optional, and will be inferred when |
66 |
| -left off. Constructs like `while` and `if` do not require parentheses |
67 |
| -around the condition (though they allow them). |
| 36 | +This is an introductory tutorial for the Rust programming language. It |
| 37 | +covers the fundamentals of the language, including the syntax, the |
| 38 | +type system and memory model, and generics. [Additional |
| 39 | +tutorials](#what-next) cover specific language features in greater |
| 40 | +depth. |
68 | 41 |
|
69 |
| -You should, however, not conclude that Rust is simply an evolution of |
70 |
| -C. As will become clear in the rest of this tutorial, it goes in quite |
71 |
| -a different direction, with efficient, strongly-typed and memory-safe |
72 |
| -support for many high-level idioms. |
| 42 | +It assumes the reader is familiar with the basic concepts of |
| 43 | +programming, and has programmed in one or more other languages |
| 44 | +before. It will often make comparisons to other languages, |
| 45 | +particularly those in the C family. |
73 | 46 |
|
74 | 47 | ## Conventions
|
75 | 48 |
|
76 | 49 | Throughout the tutorial, words that indicate language keywords or
|
77 |
| -identifiers defined in the example code are displayed in `code font`. |
| 50 | +identifiers defined in example code are displayed in `code font`. |
78 | 51 |
|
79 | 52 | Code snippets are indented, and also shown in a monospaced font. Not
|
80 | 53 | all snippets constitute whole programs. For brevity, we'll often show
|
|
0 commit comments