Skip to content

Commit 251eeb2

Browse files
authored
Merge pull request #1651 from adrian5/cleanup
Clean up Chapter 2 (Primitives)
2 parents 995df09 + 29ee37f commit 251eeb2

File tree

4 files changed

+102
-97
lines changed

4 files changed

+102
-97
lines changed

src/primitives.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,27 @@
22

33
Rust provides access to a wide variety of `primitives`. A sample includes:
44

5-
65
### Scalar Types
76

8-
* signed integers: `i8`, `i16`, `i32`, `i64`, `i128` and `isize` (pointer size)
9-
* unsigned integers: `u8`, `u16`, `u32`, `u64`, `u128` and `usize` (pointer
7+
* Signed integers: `i8`, `i16`, `i32`, `i64`, `i128` and `isize` (pointer size)
8+
* Unsigned integers: `u8`, `u16`, `u32`, `u64`, `u128` and `usize` (pointer
109
size)
11-
* floating point: `f32`, `f64`
10+
* Floating point: `f32`, `f64`
1211
* `char` Unicode scalar values like `'a'`, `'α'` and `'∞'` (4 bytes each)
1312
* `bool` either `true` or `false`
14-
* and the unit type `()`, whose only possible value is an empty tuple: `()`
13+
* The unit type `()`, whose only possible value is an empty tuple: `()`
1514

16-
Despite the value of a unit type being a tuple, it is not considered a
17-
compound type because it does not contain multiple values.
15+
Despite the value of a unit type being a tuple, it is not considered a compound
16+
type because it does not contain multiple values.
1817

1918
### Compound Types
2019

21-
* arrays like `[1, 2, 3]`
22-
* tuples like `(1, true)`
20+
* Arrays like `[1, 2, 3]`
21+
* Tuples like `(1, true)`
2322

24-
Variables can always be *type annotated*. Numbers may additionally be
25-
annotated via a *suffix* or *by default*. Integers default to `i32` and
26-
floats to `f64`. Note that Rust can also infer types from context.
23+
Variables can always be *type annotated*. Numbers may additionally be annotated
24+
via a *suffix* or *by default*. Integers default to `i32` and floats to `f64`.
25+
Note that Rust can also infer types from context.
2726

2827
```rust,editable,ignore,mdbook-runnable
2928
fn main() {
@@ -36,26 +35,27 @@ fn main() {
3635
// Or a default will be used.
3736
let default_float = 3.0; // `f64`
3837
let default_integer = 7; // `i32`
39-
40-
// A type can also be inferred from context
41-
let mut inferred_type = 12; // Type i64 is inferred from another line
38+
39+
// A type can also be inferred from context.
40+
let mut inferred_type = 12; // Type i64 is inferred from another line.
4241
inferred_type = 4294967296i64;
43-
42+
4443
// A mutable variable's value can be changed.
4544
let mut mutable = 12; // Mutable `i32`
4645
mutable = 21;
47-
46+
4847
// Error! The type of a variable can't be changed.
4948
mutable = true;
50-
49+
5150
// Variables can be overwritten with shadowing.
5251
let mutable = true;
5352
}
5453
```
5554

5655
### See also:
5756

58-
[the `std` library][std], [`mut`][mut], [`inference`][inference], and [`shadowing`][shadowing]
57+
[the `std` library][std], [`mut`][mut], [`inference`][inference], and
58+
[`shadowing`][shadowing]
5959

6060
[std]: https://doc.rust-lang.org/std/
6161
[mut]: variable_bindings/mut.md

src/primitives/array.md

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,66 +5,65 @@ memory. Arrays are created using brackets `[]`, and their length, which is known
55
at compile time, is part of their type signature `[T; length]`.
66

77
Slices are similar to arrays, but their length is not known at compile time.
8-
Instead, a slice is a two-word object, the first word is a pointer to the data,
9-
and the second word is the length of the slice. The word size is the same as
10-
usize, determined by the processor architecture e.g. 64 bits on an x86-64.
11-
Slices can be used to borrow a section of an array, and have the type signature
12-
`&[T]`.
8+
Instead, a slice is a two-word object; the first word is a pointer to the data,
9+
the second word the length of the slice. The word size is the same as usize,
10+
determined by the processor architecture, e.g. 64 bits on an x86-64. Slices can
11+
be used to borrow a section of an array and have the type signature `&[T]`.
1312

1413
```rust,editable,ignore,mdbook-runnable
1514
use std::mem;
1615
17-
// This function borrows a slice
16+
// This function borrows a slice.
1817
fn analyze_slice(slice: &[i32]) {
19-
println!("first element of the slice: {}", slice[0]);
20-
println!("the slice has {} elements", slice.len());
18+
println!("First element of the slice: {}", slice[0]);
19+
println!("The slice has {} elements", slice.len());
2120
}
2221
2322
fn main() {
24-
// Fixed-size array (type signature is superfluous)
23+
// Fixed-size array (type signature is superfluous).
2524
let xs: [i32; 5] = [1, 2, 3, 4, 5];
2625
27-
// All elements can be initialized to the same value
26+
// All elements can be initialized to the same value.
2827
let ys: [i32; 500] = [0; 500];
2928
30-
// Indexing starts at 0
31-
println!("first element of the array: {}", xs[0]);
32-
println!("second element of the array: {}", xs[1]);
29+
// Indexing starts at 0.
30+
println!("First element of the array: {}", xs[0]);
31+
println!("Second element of the array: {}", xs[1]);
3332
34-
// `len` returns the count of elements in the array
35-
println!("number of elements in array: {}", xs.len());
33+
// `len` returns the count of elements in the array.
34+
println!("Number of elements in array: {}", xs.len());
3635
37-
// Arrays are stack allocated
38-
println!("array occupies {} bytes", mem::size_of_val(&xs));
36+
// Arrays are stack allocated.
37+
println!("Array occupies {} bytes", mem::size_of_val(&xs));
3938
40-
// Arrays can be automatically borrowed as slices
41-
println!("borrow the whole array as a slice");
39+
// Arrays can be automatically borrowed as slices.
40+
println!("Borrow the whole array as a slice.");
4241
analyze_slice(&xs);
4342
44-
// Slices can point to a section of an array
45-
// They are of the form [starting_index..ending_index]
46-
// starting_index is the first position in the slice
47-
// ending_index is one more than the last position in the slice
48-
println!("borrow a section of the array as a slice");
43+
// Slices can point to a section of an array.
44+
// They are of the form [starting_index..ending_index].
45+
// `starting_index` is the first position in the slice.
46+
// `ending_index` is one more than the last position in the slice.
47+
println!("Borrow a section of the array as a slice.");
4948
analyze_slice(&ys[1 .. 4]);
5049
51-
// Example of empty slice `&[]`
50+
// Example of empty slice `&[]`:
5251
let empty_array: [u32; 0] = [];
5352
assert_eq!(&empty_array, &[]);
54-
assert_eq!(&empty_array, &[][..]); // same but more verbose
53+
assert_eq!(&empty_array, &[][..]); // Same but more verbose
5554
5655
// Arrays can be safely accessed using `.get`, which returns an
5756
// `Option`. This can be matched as shown below, or used with
5857
// `.expect()` if you would like the program to exit with a nice
5958
// message instead of happily continue.
60-
for i in 0..xs.len() + 1 { // OOPS, one element too far
59+
for i in 0..xs.len() + 1 { // Oops, one element too far!
6160
match xs.get(i) {
6261
Some(xval) => println!("{}: {}", i, xval),
6362
None => println!("Slow down! {} is too far!", i),
6463
}
6564
}
6665
67-
// Out of bound indexing causes runtime error
66+
// Out of bound indexing causes runtime error.
6867
//println!("{}", xs[5]);
6968
}
7069
```

src/primitives/literals.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ notation using these prefixes respectively: `0x`, `0o` or `0b`.
99
Underscores can be inserted in numeric literals to improve readability, e.g.
1010
`1_000` is the same as `1000`, and `0.000_001` is the same as `0.000001`.
1111

12+
Rust also supports scientific [E-notation][enote], e.g. `1e6`, `7.6e-4`. The
13+
associated type is `f64`.
14+
1215
We need to tell the compiler the type of the literals we use. For now,
1316
we'll use the `u32` suffix to indicate that the literal is an unsigned 32-bit
1417
integer, and the `i32` suffix to indicate that it's a signed 32-bit integer.
1518

16-
The operators available and their precedence [in Rust][rust op-prec] are similar to other
17-
[C-like languages][op-prec].
19+
The operators available and their precedence [in Rust][rust op-prec] are similar
20+
to other [C-like languages][op-prec].
1821

1922
```rust,editable
2023
fn main() {
@@ -25,6 +28,9 @@ fn main() {
2528
println!("1 - 2 = {}", 1i32 - 2);
2629
// TODO ^ Try changing `1i32` to `1u32` to see why the type is important
2730
31+
// Scientific notation
32+
println!("1e4 is {}, -2.5e-3 is {}", 1e4, -2.5e-3);
33+
2834
// Short-circuiting boolean logic
2935
println!("true AND false is {}", true && false);
3036
println!("true OR false is {}", true || false);
@@ -42,5 +48,6 @@ fn main() {
4248
}
4349
```
4450

51+
[enote]: https://en.wikipedia.org/wiki/Scientific_notation#E_notation
4552
[rust op-prec]: https://doc.rust-lang.org/reference/expressions.html#expression-precedence
4653
[op-prec]: https://en.wikipedia.org/wiki/Operator_precedence#Programming_languages

src/primitives/tuples.md

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ using parentheses `()`, and each tuple itself is a value with type signature
66
use tuples to return multiple values, as tuples can hold any number of values.
77

88
```rust,editable
9-
// Tuples can be used as function arguments and as return values
9+
// Tuples can be used as function arguments and as return values.
1010
fn reverse(pair: (i32, bool)) -> (bool, i32) {
11-
// `let` can be used to bind the members of a tuple to variables
11+
// `let` can be used to bind the members of a tuple to variables.
1212
let (int_param, bool_param) = pair;
1313
1414
(bool_param, int_param)
@@ -19,79 +19,78 @@ fn reverse(pair: (i32, bool)) -> (bool, i32) {
1919
struct Matrix(f32, f32, f32, f32);
2020
2121
fn main() {
22-
// A tuple with a bunch of different types
22+
// A tuple with a bunch of different types.
2323
let long_tuple = (1u8, 2u16, 3u32, 4u64,
2424
-1i8, -2i16, -3i32, -4i64,
2525
0.1f32, 0.2f64,
2626
'a', true);
2727
28-
// Values can be extracted from the tuple using tuple indexing
29-
println!("long tuple first value: {}", long_tuple.0);
30-
println!("long tuple second value: {}", long_tuple.1);
28+
// Values can be extracted from the tuple using tuple indexing.
29+
println!("Long tuple first value: {}", long_tuple.0);
30+
println!("Long tuple second value: {}", long_tuple.1);
3131
32-
// Tuples can be tuple members
32+
// Tuples can be tuple members.
3333
let tuple_of_tuples = ((1u8, 2u16, 2u32), (4u64, -1i8), -2i16);
3434
35-
// Tuples are printable
35+
// Tuples are printable.
3636
println!("tuple of tuples: {:?}", tuple_of_tuples);
37-
38-
// But long Tuples (more than 12 elements) cannot be printed
39-
// let too_long_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
40-
// println!("too long tuple: {:?}", too_long_tuple);
37+
38+
// But long Tuples (more than 12 elements) cannot be printed.
39+
//let too_long_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
40+
//println!("Too long tuple: {:?}", too_long_tuple);
4141
// TODO ^ Uncomment the above 2 lines to see the compiler error
4242
4343
let pair = (1, true);
44-
println!("pair is {:?}", pair);
44+
println!("Pair is {:?}", pair);
4545
46-
println!("the reversed pair is {:?}", reverse(pair));
46+
println!("Uhe reversed pair is {:?}", reverse(pair));
4747
4848
// To create one element tuples, the comma is required to tell them apart
49-
// from a literal surrounded by parentheses
50-
println!("one element tuple: {:?}", (5u32,));
51-
println!("just an integer: {:?}", (5u32));
49+
// from a literal surrounded by parentheses.
50+
println!("One element tuple: {:?}", (5u32,));
51+
println!("Just an integer: {:?}", (5u32));
5252
53-
//tuples can be destructured to create bindings
53+
// Tuples can be destructured to create bindings.
5454
let tuple = (1, "hello", 4.5, true);
5555
5656
let (a, b, c, d) = tuple;
5757
println!("{:?}, {:?}, {:?}, {:?}", a, b, c, d);
5858
5959
let matrix = Matrix(1.1, 1.2, 2.1, 2.2);
6060
println!("{:?}", matrix);
61-
6261
}
6362
```
6463

6564
### Activity
6665

67-
1. *Recap*: Add the `fmt::Display` trait to the `Matrix` struct in the above example,
68-
so that if you switch from printing the debug format `{:?}` to the display
69-
format `{}`, you see the following output:
70-
71-
```text
72-
( 1.1 1.2 )
73-
( 2.1 2.2 )
74-
```
75-
76-
You may want to refer back to the example for [print display][print_display].
77-
2. Add a `transpose` function using the `reverse` function as a template, which
78-
accepts a matrix as an argument, and returns a matrix in which two elements
79-
have been swapped. For example:
80-
81-
```rust,ignore
82-
println!("Matrix:\n{}", matrix);
83-
println!("Transpose:\n{}", transpose(matrix));
84-
```
85-
86-
results in the output:
87-
88-
```text
89-
Matrix:
90-
( 1.1 1.2 )
91-
( 2.1 2.2 )
92-
Transpose:
93-
( 1.1 2.1 )
94-
( 1.2 2.2 )
95-
```
66+
1. *Recap*: Add the `fmt::Display` trait to the `Matrix` struct in the above
67+
example, so that if you switch from printing the debug format `{:?}` to the
68+
display format `{}`, you see the following output:
69+
70+
```text
71+
( 1.1 1.2 )
72+
( 2.1 2.2 )
73+
```
74+
75+
You may want to refer back to the example for [print display][print_display].
76+
2. Add a `transpose` function using the `reverse` function as a template, which
77+
accepts a matrix as an argument, and returns a matrix in which two elements
78+
have been swapped. For example:
79+
80+
```rust,ignore
81+
println!("Matrix:\n{}", matrix);
82+
println!("Transpose:\n{}", transpose(matrix));
83+
```
84+
85+
Results in the output:
86+
87+
```text
88+
Matrix:
89+
( 1.1 1.2 )
90+
( 2.1 2.2 )
91+
Transpose:
92+
( 1.1 2.1 )
93+
( 1.2 2.2 )
94+
```
9695

9796
[print_display]: ../hello/print/print_display.md

0 commit comments

Comments
 (0)