]> git.proxmox.com Git - rustc.git/blob - src/doc/book/src/ch03-02-data-types.md
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / src / doc / book / src / ch03-02-data-types.md
1 ## Data Types
2
3 Every value in Rust is of a certain *data type*, which tells Rust what kind of
4 data is being specified so it knows how to work with that data. We’ll look at
5 two data type subsets: scalar and compound.
6
7 Keep in mind that Rust is a *statically typed* language, which means that it
8 must know the types of all variables at compile time. The compiler can usually
9 infer what type we want to use based on the value and how we use it. In cases
10 when many types are possible, such as when we converted a `String` to a numeric
11 type using `parse` in the [“Comparing the Guess to the Secret
12 Number”][comparing-the-guess-to-the-secret-number]<!-- ignore --> section in
13 Chapter 2, we must add a type annotation, like this:
14
15 ```rust
16 let guess: u32 = "42".parse().expect("Not a number!");
17 ```
18
19 If we don’t add the type annotation here, Rust will display the following
20 error, which means the compiler needs more information from us to know which
21 type we want to use:
22
23 ```console
24 {{#include ../listings/ch03-common-programming-concepts/output-only-01-no-type-annotations/output.txt}}
25 ```
26
27 You’ll see different type annotations for other data types.
28
29 ### Scalar Types
30
31 A *scalar* type represents a single value. Rust has four primary scalar types:
32 integers, floating-point numbers, Booleans, and characters. You may recognize
33 these from other programming languages. Let’s jump into how they work in Rust.
34
35 #### Integer Types
36
37 An *integer* is a number without a fractional component. We used one integer
38 type in Chapter 2, the `u32` type. This type declaration indicates that the
39 value it’s associated with should be an unsigned integer (signed integer types
40 start with `i`, instead of `u`) that takes up 32 bits of space. Table 3-1 shows
41 the built-in integer types in Rust. Each variant in the Signed and Unsigned
42 columns (for example, `i16`) can be used to declare the type of an integer
43 value.
44
45 <span class="caption">Table 3-1: Integer Types in Rust</span>
46
47 | Length | Signed | Unsigned |
48 |---------|---------|----------|
49 | 8-bit | `i8` | `u8` |
50 | 16-bit | `i16` | `u16` |
51 | 32-bit | `i32` | `u32` |
52 | 64-bit | `i64` | `u64` |
53 | 128-bit | `i128` | `u128` |
54 | arch | `isize` | `usize` |
55
56 Each variant can be either signed or unsigned and has an explicit size.
57 *Signed* and *unsigned* refer to whether it’s possible for the number to be
58 negative—in other words, whether the number needs to have a sign
59 with it (signed) or whether it will only ever be positive and can therefore be
60 represented without a sign (unsigned). It’s like writing numbers on paper: when
61 the sign matters, a number is shown with a plus sign or a minus sign; however,
62 when it’s safe to assume the number is positive, it’s shown with no sign.
63 Signed numbers are stored using [two’s complement](https://en.wikipedia.org/wiki/Two%27s_complement) representation.
64
65 Each signed variant can store numbers from -(2<sup>n - 1</sup>) to 2<sup>n -
66 1</sup> - 1 inclusive, where *n* is the number of bits that variant uses. So an
67 `i8` can store numbers from -(2<sup>7</sup>) to 2<sup>7</sup> - 1, which equals
68 -128 to 127. Unsigned variants can store numbers from 0 to 2<sup>n</sup> - 1,
69 so a `u8` can store numbers from 0 to 2<sup>8</sup> - 1, which equals 0 to 255.
70
71 Additionally, the `isize` and `usize` types depend on the kind of computer your
72 program is running on: 64 bits if you’re on a 64-bit architecture and 32 bits
73 if you’re on a 32-bit architecture.
74
75 You can write integer literals in any of the forms shown in Table 3-2. Note
76 that all number literals except the byte literal allow a type suffix, such as
77 `57u8`, and `_` as a visual separator, such as `1_000`.
78
79 <span class="caption">Table 3-2: Integer Literals in Rust</span>
80
81 | Number literals | Example |
82 |------------------|---------------|
83 | Decimal | `98_222` |
84 | Hex | `0xff` |
85 | Octal | `0o77` |
86 | Binary | `0b1111_0000` |
87 | Byte (`u8` only) | `b'A'` |
88
89 So how do you know which type of integer to use? If you’re unsure, Rust’s
90 defaults are generally good choices, and integer types default to `i32`: this
91 type is generally the fastest, even on 64-bit systems. The primary situation in
92 which you’d use `isize` or `usize` is when indexing some sort of collection.
93
94 > ##### Integer Overflow
95 >
96 > Let’s say you have a variable of type `u8` that can hold values between 0 and 255.
97 > If you try to change the variable to a value outside of that range, such
98 > as 256, *integer overflow* will occur. Rust has some interesting rules
99 > involving this behavior. When you’re compiling in debug mode, Rust includes
100 > checks for integer overflow that cause your program to *panic* at runtime if
101 > this behavior occurs. Rust uses the term panicking when a program exits with
102 > an error; we’ll discuss panics in more depth in the [“Unrecoverable Errors
103 > with `panic!`”][unrecoverable-errors-with-panic]<!-- ignore --> section in
104 > Chapter 9.
105 >
106 > When you’re compiling in release mode with the `--release` flag, Rust does
107 > *not* include checks for integer overflow that cause panics. Instead, if
108 > overflow occurs, Rust performs *two’s complement wrapping*. In short, values
109 > greater than the maximum value the type can hold “wrap around” to the minimum
110 > of the values the type can hold. In the case of a `u8`, 256 becomes 0, 257
111 > becomes 1, and so on. The program won’t panic, but the variable will have a
112 > value that probably isn’t what you were expecting it to have. Relying on
113 > integer overflow’s wrapping behavior is considered an error. If you want to
114 > wrap explicitly, you can use the standard library type [`Wrapping`][wrapping].
115
116 #### Floating-Point Types
117
118 Rust also has two primitive types for *floating-point numbers*, which are
119 numbers with decimal points. Rust’s floating-point types are `f32` and `f64`,
120 which are 32 bits and 64 bits in size, respectively. The default type is `f64`
121 because on modern CPUs it’s roughly the same speed as `f32` but is capable of
122 more precision.
123
124 Here’s an example that shows floating-point numbers in action:
125
126 <span class="filename">Filename: src/main.rs</span>
127
128 ```rust
129 {{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-06-floating-point/src/main.rs}}
130 ```
131
132 Floating-point numbers are represented according to the IEEE-754 standard. The
133 `f32` type is a single-precision float, and `f64` has double precision.
134
135 #### Numeric Operations
136
137 Rust supports the basic mathematical operations you’d expect for all of the
138 number types: addition, subtraction, multiplication, division, and remainder.
139 The following code shows how you’d use each one in a `let` statement:
140
141 <span class="filename">Filename: src/main.rs</span>
142
143 ```rust
144 {{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-07-numeric-operations/src/main.rs}}
145 ```
146
147 Each expression in these statements uses a mathematical operator and evaluates
148 to a single value, which is then bound to a variable. Appendix B contains a
149 list of all operators that Rust provides.
150
151 #### The Boolean Type
152
153 As in most other programming languages, a Boolean type in Rust has two possible
154 values: `true` and `false`. Booleans are one byte in size. The Boolean type in
155 Rust is specified using `bool`. For example:
156
157 <span class="filename">Filename: src/main.rs</span>
158
159 ```rust
160 {{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-08-boolean/src/main.rs}}
161 ```
162
163 The main way to use Boolean values is through conditionals, such as an `if`
164 expression. We’ll cover how `if` expressions work in Rust in the [“Control
165 Flow”][control-flow]<!-- ignore --> section.
166
167 #### The Character Type
168
169 So far we’ve worked only with numbers, but Rust supports letters too. Rust’s
170 `char` type is the language’s most primitive alphabetic type, and the following
171 code shows one way to use it. (Note that `char` literals are specified with
172 single quotes, as opposed to string literals, which use double quotes.)
173
174 <span class="filename">Filename: src/main.rs</span>
175
176 ```rust
177 {{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-09-char/src/main.rs}}
178 ```
179
180 Rust’s `char` type is four bytes in size and represents a Unicode Scalar Value,
181 which means it can represent a lot more than just ASCII. Accented letters;
182 Chinese, Japanese, and Korean characters; emoji; and zero-width spaces are all
183 valid `char` values in Rust. Unicode Scalar Values range from `U+0000` to
184 `U+D7FF` and `U+E000` to `U+10FFFF` inclusive. However, a “character” isn’t
185 really a concept in Unicode, so your human intuition for what a “character” is
186 may not match up with what a `char` is in Rust. We’ll discuss this topic in
187 detail in [“Storing UTF-8 Encoded Text with Strings”][strings]<!-- ignore -->
188 in Chapter 8.
189
190 ### Compound Types
191
192 *Compound types* can group multiple values into one type. Rust has two
193 primitive compound types: tuples and arrays.
194
195 #### The Tuple Type
196
197 A tuple is a general way of grouping together a number of values with a variety
198 of types into one compound type. Tuples have a fixed length: once declared,
199 they cannot grow or shrink in size.
200
201 We create a tuple by writing a comma-separated list of values inside
202 parentheses. Each position in the tuple has a type, and the types of the
203 different values in the tuple don’t have to be the same. We’ve added optional
204 type annotations in this example:
205
206 <span class="filename">Filename: src/main.rs</span>
207
208 ```rust
209 {{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-10-tuples/src/main.rs}}
210 ```
211
212 The variable `tup` binds to the entire tuple, because a tuple is considered a
213 single compound element. To get the individual values out of a tuple, we can
214 use pattern matching to destructure a tuple value, like this:
215
216 <span class="filename">Filename: src/main.rs</span>
217
218 ```rust
219 {{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-11-destructuring-tuples/src/main.rs}}
220 ```
221
222 This program first creates a tuple and binds it to the variable `tup`. It then
223 uses a pattern with `let` to take `tup` and turn it into three separate
224 variables, `x`, `y`, and `z`. This is called *destructuring*, because it breaks
225 the single tuple into three parts. Finally, the program prints the value of
226 `y`, which is `6.4`.
227
228 In addition to destructuring through pattern matching, we can access a tuple
229 element directly by using a period (`.`) followed by the index of the value we
230 want to access. For example:
231
232 <span class="filename">Filename: src/main.rs</span>
233
234 ```rust
235 {{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-12-tuple-indexing/src/main.rs}}
236 ```
237
238 This program creates a tuple, `x`, and then makes new variables for each
239 element by using their respective indices. As with most programming languages,
240 the first index in a tuple is 0.
241
242 #### The Array Type
243
244 Another way to have a collection of multiple values is with an *array*. Unlike
245 a tuple, every element of an array must have the same type. Arrays in Rust are
246 different from arrays in some other languages because arrays in Rust have a
247 fixed length, like tuples.
248
249 In Rust, the values going into an array are written as a comma-separated list
250 inside square brackets:
251
252 <span class="filename">Filename: src/main.rs</span>
253
254 ```rust
255 {{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-13-arrays/src/main.rs}}
256 ```
257
258 Arrays are useful when you want your data allocated on the stack rather than
259 the heap (we will discuss the stack and the heap more in Chapter 4) or when
260 you want to ensure you always have a fixed number of elements. An array isn’t
261 as flexible as the vector type, though. A vector is a similar collection type
262 provided by the standard library that *is* allowed to grow or shrink in size.
263 If you’re unsure whether to use an array or a vector, you should probably use a
264 vector. Chapter 8 discusses vectors in more detail.
265
266 An example of when you might want to use an array rather than a vector is in a
267 program that needs to know the names of the months of the year. It’s very
268 unlikely that such a program will need to add or remove months, so you can use
269 an array because you know it will always contain 12 elements:
270
271 ```rust
272 let months = ["January", "February", "March", "April", "May", "June", "July",
273 "August", "September", "October", "November", "December"];
274 ```
275
276 You would write an array’s type by using square brackets, and within the
277 brackets include the type of each element, a semicolon, and then the number of
278 elements in the array, like so:
279
280 ```rust
281 let a: [i32; 5] = [1, 2, 3, 4, 5];
282 ```
283
284 Here, `i32` is the type of each element. After the semicolon, the number `5`
285 indicates the array contains five elements.
286
287 Writing an array’s type this way looks similar to an alternative syntax for
288 initializing an array: if you want to create an array that contains the same
289 value for each element, you can specify the initial value, followed by a
290 semicolon, and then the length of the array in square brackets, as shown here:
291
292 ```rust
293 let a = [3; 5];
294 ```
295
296 The array named `a` will contain `5` elements that will all be set to the value
297 `3` initially. This is the same as writing `let a = [3, 3, 3, 3, 3];` but in a
298 more concise way.
299
300 ##### Accessing Array Elements
301
302 An array is a single chunk of memory allocated on the stack. You can access
303 elements of an array using indexing, like this:
304
305 <span class="filename">Filename: src/main.rs</span>
306
307 ```rust
308 {{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-14-array-indexing/src/main.rs}}
309 ```
310
311 In this example, the variable named `first` will get the value `1`, because
312 that is the value at index `[0]` in the array. The variable named `second` will
313 get the value `2` from index `[1]` in the array.
314
315 ##### Invalid Array Element Access
316
317 What happens if you try to access an element of an array that is past the end
318 of the array? Say you change the example to the following code, which will
319 compile but exit with an error when it runs:
320
321 <span class="filename">Filename: src/main.rs</span>
322
323 ```rust,ignore,does_not_compile
324 {{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/src/main.rs}}
325 ```
326
327 Running this code using `cargo run` produces the following result:
328
329 ```console
330 {{#include ../listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/output.txt}}
331 ```
332
333 The compilation didn’t produce any errors, but the program resulted in a
334 *runtime* error and didn’t exit successfully. When you attempt to access an
335 element using indexing, Rust will check that the index you’ve specified is less
336 than the array length. If the index is greater than or equal to the array
337 length, Rust will panic.
338
339 This is the first example of Rust’s safety principles in action. In many
340 low-level languages, this kind of check is not done, and when you provide an
341 incorrect index, invalid memory can be accessed. Rust protects you against this
342 kind of error by immediately exiting instead of allowing the memory access and
343 continuing. Chapter 9 discusses more of Rust’s error handling.
344
345 [comparing-the-guess-to-the-secret-number]:
346 ch02-00-guessing-game-tutorial.html#comparing-the-guess-to-the-secret-number
347 [control-flow]: ch03-05-control-flow.html#control-flow
348 [strings]: ch08-02-strings.html#storing-utf-8-encoded-text-with-strings
349 [unrecoverable-errors-with-panic]: ch09-01-unrecoverable-errors-with-panic.html
350 [wrapping]: ../std/num/struct.Wrapping.html