You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: book/src/02_basic_calculator/01_integers.md
+13-13
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Types, part 1
2
2
3
-
In the ["Syntax" section](../01_intro/01_syntax.md)`compute`'s input parameters were of type `u32`.
3
+
In the ["Syntax" section](../01_intro/01_syntax.md)`compute`'s input parameters were of type `u32`.\
4
4
Let's unpack what that _means_.
5
5
6
6
## Primitive types
@@ -18,25 +18,25 @@ An integer is a number that can be written without a fractional component. E.g.
18
18
19
19
### Signed vs. unsigned
20
20
21
-
An integer can be **signed** or **unsigned**.
21
+
An integer can be **signed** or **unsigned**.\
22
22
An unsigned integer can only represent non-negative numbers (i.e. `0` or greater).
23
23
A signed integer can represent both positive and negative numbers (e.g. `-1`, `12`, etc.).
24
24
25
-
The `u` in `u32` stands for **unsigned**.
25
+
The `u` in `u32` stands for **unsigned**.\
26
26
The equivalent type for signed integer is `i32`, where the `i` stands for integer (i.e. any integer, positive or
27
27
negative).
28
28
29
29
### Bit width
30
30
31
-
The `32` in `u32` refers to the **number of bits[^bit]** used to represent the number in memory.
31
+
The `32` in `u32` refers to the **number of bits[^bit]** used to represent the number in memory.\
32
32
The more bits, the larger the range of numbers that can be represented.
33
33
34
34
Rust supports multiple bit widths for integers: `8`, `16`, `32`, `64`, `128`.
35
35
36
-
With 32 bits, `u32` can represent numbers from `0` to `2^32 - 1` (a.k.a. [`u32::MAX`](https://doc.rust-lang.org/std/primitive.u32.html#associatedconstant.MAX)).
36
+
With 32 bits, `u32` can represent numbers from `0` to `2^32 - 1` (a.k.a. [`u32::MAX`](https://doc.rust-lang.org/std/primitive.u32.html#associatedconstant.MAX)).\
37
37
With the same number of bits, a signed integer (`i32`) can represent numbers from `-2^31` to `2^31 - 1`
38
38
(i.e. from [`i32::MIN`](https://doc.rust-lang.org/std/primitive.i32.html#associatedconstant.MIN)
39
-
to [`i32::MAX`](https://doc.rust-lang.org/std/primitive.i32.html#associatedconstant.MAX)).
39
+
to [`i32::MAX`](https://doc.rust-lang.org/std/primitive.i32.html#associatedconstant.MAX)).\
40
40
The maximum value for `i32` is smaller than the maximum value for `u32` because one bit is used to represent
41
41
the sign of the number. Check out the [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement)
42
42
representation for more details on how signed integers are represented in memory.
@@ -46,7 +46,7 @@ representation for more details on how signed integers are represented in memory
46
46
Combining the two variables (signed/unsigned and bit width), we get the following integer types:
47
47
48
48
| Bit width | Signed | Unsigned |
49
-
|-----------|--------|----------|
49
+
|---------|------|--------|
50
50
| 8-bit |`i8`|`u8`|
51
51
| 16-bit |`i16`|`u16`|
52
52
| 32-bit |`i32`|`u32`|
@@ -55,21 +55,21 @@ Combining the two variables (signed/unsigned and bit width), we get the followin
55
55
56
56
## Literals
57
57
58
-
A **literal** is a notation for representing a fixed value in source code.
58
+
A **literal** is a notation for representing a fixed value in source code.\
59
59
For example, `42` is a Rust literal for the number forty-two.
60
60
61
61
### Type annotations for literals
62
62
63
63
But all values in Rust have a type, so... what's the type of `42`?
64
64
65
-
The Rust compiler will try to infer the type of a literal based on how it's used.
66
-
If you don't provide any context, the compiler will default to `i32` for integer literals.
65
+
The Rust compiler will try to infer the type of a literal based on how it's used.\
66
+
If you don't provide any context, the compiler will default to `i32` for integer literals.\
67
67
If you want to use a different type, you can add the desired integer type as a suffix—e.g. `2u64` is a 2 that's
68
68
explicitly typed as a `u64`.
69
69
70
70
### Underscores in literals
71
71
72
-
You can use underscores `_` to improve the readability of large numbers.
72
+
You can use underscores `_` to improve the readability of large numbers.\
73
73
For example, `1_000_000` is the same as `1000000`.
74
74
75
75
## Arithmetic operators
@@ -82,7 +82,7 @@ Rust supports the following arithmetic operators[^traits] for integers:
82
82
-`/` for division
83
83
-`%` for remainder
84
84
85
-
Precedence and associativity rules for these operators are the same as in mathematics.
85
+
Precedence and associativity rules for these operators are the same as in mathematics.\
86
86
You can use parentheses to override the default precedence. E.g. `2 * (3 + 4)`.
87
87
88
88
> ⚠️ **Warning**
@@ -92,7 +92,7 @@ You can use parentheses to override the default precedence. E.g. `2 * (3 + 4)`.
92
92
93
93
## No automatic type coercion
94
94
95
-
As we discussed in the previous exercise, Rust is a statically typed language.
95
+
As we discussed in the previous exercise, Rust is a statically typed language.\
96
96
In particular, Rust is quite strict about type coercion. It won't automatically convert a value from one type to
97
97
another[^coercion],
98
98
even if the conversion is lossless. You have to do it explicitly.
0 commit comments