Skip to content

Commit 99591a7

Browse files
Formatter (mainmatter#51)
Enforce consistent formatting use `dprint`
1 parent 5371185 commit 99591a7

File tree

157 files changed

+1057
-1044
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

157 files changed

+1057
-1044
lines changed

.github/workflows/ci.yml

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ on:
99
- main
1010

1111
jobs:
12+
formatter:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v3
16+
- uses: dprint/[email protected]
17+
1218
check-links:
1319
runs-on: ubuntu-latest
1420
steps:

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# Learn Rust, one exercise at a time
22

3-
You've heard about Rust, but you never had the chance to try it out?
3+
You've heard about Rust, but you never had the chance to try it out?\
44
This course is for you!
55

6-
You'll learn Rust by solving 100 exercises.
6+
You'll learn Rust by solving 100 exercises.\
77
You'll go from knowing nothing about Rust to being able to start
88
writing your own programs, one exercise at a time.
99

1010
> [!NOTE]
11-
> This course has been written by [Mainmatter](https://mainmatter.com/rust-consulting/).
12-
> It's one of the trainings in [our portfolio of Rust workshops](https://mainmatter.com/services/workshops/rust/).
11+
> This course has been written by [Mainmatter](https://mainmatter.com/rust-consulting/).\
12+
> It's one of the trainings in [our portfolio of Rust workshops](https://mainmatter.com/services/workshops/rust/).\
1313
> Check out our [landing page](https://mainmatter.com/rust-consulting/) if you're looking for Rust consulting or
1414
> training!
1515
@@ -20,7 +20,7 @@ to get started with the course.
2020

2121
## Requirements
2222

23-
- **Rust** (follow instructions [here](https://www.rust-lang.org/tools/install)).
23+
- **Rust** (follow instructions [here](https://www.rust-lang.org/tools/install)).\
2424
If `rustup` is already installed on your system, run `rustup update` (or another appropriate command depending on how
2525
you installed Rust on your system)
2626
to make sure you're running on the latest stable version.

book/src/01_intro/00_welcome.md

+26-26
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
# Welcome
22

3-
Welcome to **"100 Exercises To Learn Rust"**!
3+
Welcome to **"100 Exercises To Learn Rust"**!
44

5-
This course will teach you Rust's core concepts, one exercise at a time.
5+
This course will teach you Rust's core concepts, one exercise at a time.\
66
You'll learn about Rust's syntax, its type system, its standard library, and its ecosystem.
77

88
We don't assume any prior knowledge of Rust, but we assume you know at least
9-
another programming language.
9+
another programming language.\
1010
We also don't assume any prior knowledge of systems programming or memory management. Those
1111
topics will be covered in the course.
1212

13-
In other words, we'll be starting from scratch!
13+
In other words, we'll be starting from scratch!\
1414
You'll build up your Rust knowledge in small, manageable steps.
1515
By the end of the course, you will have solved ~100 exercises, enough to
1616
feel comfortable working on small to medium-sized Rust projects.
1717

1818
## Methodology
1919

20-
This course is based on the "learn by doing" principle.
21-
It has been designed to be interactive and hands-on.
20+
This course is based on the "learn by doing" principle.\
21+
It has been designed to be interactive and hands-on.
2222

2323
[Mainmatter](https://mainmatter.com/rust-consulting/) developed this course
24-
to be delivered in a classroom setting, over 4 days: each attendee advances
25-
through the lessons at their own pace, with an experienced instructor providing
26-
guidance, answering questions and diving deeper into the topics as needed.
24+
to be delivered in a classroom setting, over 4 days: each attendee advances
25+
through the lessons at their own pace, with an experienced instructor providing
26+
guidance, answering questions and diving deeper into the topics as needed.\
2727
If you're interested in attending one of our training sessions, or if you'd like to
2828
bring this course to your company, please [get in touch](https://mainmatter.com/contact/).
2929

30-
You can also follow the course on your own, but we recommend you find a friend or
31-
a mentor to help you along the way should you get stuck. You can
32-
also find solutions to all exercises in the
30+
You can also follow the course on your own, but we recommend you find a friend or
31+
a mentor to help you along the way should you get stuck. You can
32+
also find solutions to all exercises in the
3333
[`solutions` branch of the GitHub repository](https://github.com/mainmatter/100-exercises-to-learn-rust/tree/solutions).
3434

3535
## Structure
3636

3737
On the left side of the screen, you can see that the course is divided into sections.
38-
Each section introduces a new concept or feature of the Rust language.
39-
To verify your understanding, each section is paired with an exercise that you need to solve.
38+
Each section introduces a new concept or feature of the Rust language.\
39+
To verify your understanding, each section is paired with an exercise that you need to solve.
4040

41-
You can find the exercises in the
42-
[companion GitHub repository](https://github.com/mainmatter/100-exercises-to-learn-rust).
41+
You can find the exercises in the
42+
[companion GitHub repository](https://github.com/mainmatter/100-exercises-to-learn-rust).\
4343
Before starting the course, make sure to clone the repository to your local machine:
4444

4545
```bash
@@ -60,13 +60,13 @@ git checkout -b my-solutions
6060

6161
All exercises are located in the `exercises` folder.
6262
Each exercise is structured as a Rust package.
63-
The package contains the exercise itself, instructions on what to do (in `src/lib.rs`), and a test suite to
63+
The package contains the exercise itself, instructions on what to do (in `src/lib.rs`), and a test suite to
6464
automatically verify your solution.
6565

6666
### `wr`, the workshop runner
6767

6868
To verify your solutions, we've provided a tool that will guide you through the course.
69-
It is the `wr` CLI (short for "workshop runner").
69+
It is the `wr` CLI (short for "workshop runner").
7070
Install it with:
7171

7272
```bash
@@ -80,10 +80,10 @@ Run the `wr` command to start the course:
8080
wr
8181
```
8282

83-
`wr` will verify the solution to the current exercise.
84-
Don't move on to the next section until you've solved the exercise for the current one.
83+
`wr` will verify the solution to the current exercise.\
84+
Don't move on to the next section until you've solved the exercise for the current one.
8585

86-
> We recommend committing your solutions to Git as you progress through the course,
86+
> We recommend committing your solutions to Git as you progress through the course,
8787
> so you can easily track your progress and "restart" from a known point if needed.
8888
8989
Enjoy the course!
@@ -95,10 +95,10 @@ Enjoy the course!
9595
## Author
9696

9797
This course was written by [Luca Palmieri](https://www.lpalmieri.com/), Principal Engineering
98-
Consultant at [Mainmatter](https://mainmatter.com/rust-consulting/).
99-
Luca has been working with Rust since 2018, initially at TrueLayer and then at AWS.
100-
Luca is the author of ["Zero to Production in Rust"](https://zero2prod.com),
101-
the go-to resource for learning how to build backend applications in Rust.
98+
Consultant at [Mainmatter](https://mainmatter.com/rust-consulting/).\
99+
Luca has been working with Rust since 2018, initially at TrueLayer and then at AWS.\
100+
Luca is the author of ["Zero to Production in Rust"](https://zero2prod.com),
101+
the go-to resource for learning how to build backend applications in Rust.\
102102
He is also the author and maintainer of a variety of open-source Rust projects, including
103103
[`cargo-chef`](https://github.com/LukeMathWalker/cargo-chef),
104-
[Pavex](https://pavex.dev) and [`wiremock`](https://github.com/LukeMathWalker/wiremock-rs).
104+
[Pavex](https://pavex.dev) and [`wiremock`](https://github.com/LukeMathWalker/wiremock-rs).

book/src/01_intro/01_syntax.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
<div class="warning">
44

5-
Don't jump ahead!
6-
Complete the exercise for the previous section before you start this one.
7-
It's located in `exercises/01_intro/00_welcome`, in the [course GitHub's repository](https://github.com/mainmatter/100-exercises-to-learn-rust).
5+
Don't jump ahead!\
6+
Complete the exercise for the previous section before you start this one.\
7+
It's located in `exercises/01_intro/00_welcome`, in the [course GitHub's repository](https://github.com/mainmatter/100-exercises-to-learn-rust).\
88
Use [`wr`](00_welcome.md#wr-the-workshop-runner) to start the course and verify your solutions.
99

1010
</div>
1111

1212
The previous task doesn't even qualify as an exercise, but it already exposed you to quite a bit of Rust **syntax**.
1313
We won't cover every single detail of Rust's syntax used in the previous exercise.
14-
Instead, we'll cover _just enough_ to keep going without getting stuck in the details.
14+
Instead, we'll cover _just enough_ to keep going without getting stuck in the details.\
1515
One step at a time!
1616

1717
## Comments
@@ -88,7 +88,7 @@ It is considered idiomatic to omit the `return` keyword when possible.
8888

8989
### Input parameters
9090

91-
Input parameters are declared inside the parentheses `()` that follow the function's name.
91+
Input parameters are declared inside the parentheses `()` that follow the function's name.\
9292
Each parameter is declared with its name, followed by a colon `:`, followed by its type.
9393

9494
For example, the `greet` function below takes a `name` parameter of type `&str` (a "string slice"):
@@ -105,10 +105,10 @@ If there are multiple input parameters, they must be separated with commas.
105105

106106
### Type annotations
107107

108-
Since we've been mentioned "types" a few times, let's state it clearly: Rust is a **statically typed language**.
108+
Since we've been mentioned "types" a few times, let's state it clearly: Rust is a **statically typed language**.\
109109
Every single value in Rust has a type and that type must be known to the compiler at compile-time.
110110

111-
Types are a form of **static analysis**.
111+
Types are a form of **static analysis**.\
112112
You can think of a type as a **tag** that the compiler attaches to every value in your program. Depending on the
113113
tag, the compiler can enforce different rules—e.g. you can't add a string to a number, but you can add two numbers
114114
together.

book/src/02_basic_calculator/00_intro.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# A Basic Calculator
22

3-
In this chapter we'll learn how to use Rust as a **calculator**.
3+
In this chapter we'll learn how to use Rust as a **calculator**.\
44
It might not sound like much, but it'll give us a chance to cover a lot of Rust's basics, such as:
55

66
- How to define and call functions

book/src/02_basic_calculator/01_integers.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Types, part 1
22

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`.\
44
Let's unpack what that _means_.
55

66
## Primitive types
@@ -18,25 +18,25 @@ An integer is a number that can be written without a fractional component. E.g.
1818

1919
### Signed vs. unsigned
2020

21-
An integer can be **signed** or **unsigned**.
21+
An integer can be **signed** or **unsigned**.\
2222
An unsigned integer can only represent non-negative numbers (i.e. `0` or greater).
2323
A signed integer can represent both positive and negative numbers (e.g. `-1`, `12`, etc.).
2424

25-
The `u` in `u32` stands for **unsigned**.
25+
The `u` in `u32` stands for **unsigned**.\
2626
The equivalent type for signed integer is `i32`, where the `i` stands for integer (i.e. any integer, positive or
2727
negative).
2828

2929
### Bit width
3030

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.\
3232
The more bits, the larger the range of numbers that can be represented.
3333

3434
Rust supports multiple bit widths for integers: `8`, `16`, `32`, `64`, `128`.
3535

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)).\
3737
With the same number of bits, a signed integer (`i32`) can represent numbers from `-2^31` to `2^31 - 1`
3838
(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)).\
4040
The maximum value for `i32` is smaller than the maximum value for `u32` because one bit is used to represent
4141
the sign of the number. Check out the [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement)
4242
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
4646
Combining the two variables (signed/unsigned and bit width), we get the following integer types:
4747

4848
| Bit width | Signed | Unsigned |
49-
|-----------|--------|----------|
49+
| --------- | ------ | -------- |
5050
| 8-bit | `i8` | `u8` |
5151
| 16-bit | `i16` | `u16` |
5252
| 32-bit | `i32` | `u32` |
@@ -55,21 +55,21 @@ Combining the two variables (signed/unsigned and bit width), we get the followin
5555

5656
## Literals
5757

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.\
5959
For example, `42` is a Rust literal for the number forty-two.
6060

6161
### Type annotations for literals
6262

6363
But all values in Rust have a type, so... what's the type of `42`?
6464

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.\
6767
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
6868
explicitly typed as a `u64`.
6969

7070
### Underscores in literals
7171

72-
You can use underscores `_` to improve the readability of large numbers.
72+
You can use underscores `_` to improve the readability of large numbers.\
7373
For example, `1_000_000` is the same as `1000000`.
7474

7575
## Arithmetic operators
@@ -82,7 +82,7 @@ Rust supports the following arithmetic operators[^traits] for integers:
8282
- `/` for division
8383
- `%` for remainder
8484

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.\
8686
You can use parentheses to override the default precedence. E.g. `2 * (3 + 4)`.
8787

8888
> ⚠️ **Warning**
@@ -92,7 +92,7 @@ You can use parentheses to override the default precedence. E.g. `2 * (3 + 4)`.
9292
9393
## No automatic type coercion
9494

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.\
9696
In particular, Rust is quite strict about type coercion. It won't automatically convert a value from one type to
9797
another[^coercion],
9898
even if the conversion is lossless. You have to do it explicitly.

book/src/02_basic_calculator/02_variables.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Variables
22

3-
In Rust, you can use the `let` keyword to declare **variables**.
3+
In Rust, you can use the `let` keyword to declare **variables**.\
44
For example:
55

66
```rust
@@ -35,20 +35,20 @@ let x = 42;
3535
let y: u32 = x;
3636
```
3737

38-
In the example above, we didn't specify the type of `x`.
38+
In the example above, we didn't specify the type of `x`.\
3939
`x` is later assigned to `y`, which is explicitly typed as `u32`. Since Rust doesn't perform automatic type coercion,
4040
the compiler infers the type of `x` to be `u32`—the same as `y` and the only type that will allow the program to compile
4141
without errors.
4242

4343
### Inference limitations
4444

45-
The compiler sometimes needs a little help to infer the correct variable type based on its usage.
45+
The compiler sometimes needs a little help to infer the correct variable type based on its usage.\
4646
In those cases you'll get a compilation error and the compiler will ask you to provide an explicit type hint to
4747
disambiguate the situation.
4848

4949
## Function arguments are variables
5050

51-
Not all heroes wear capes, not all variables are declared with `let`.
51+
Not all heroes wear capes, not all variables are declared with `let`.\
5252
Function arguments are variables too!
5353

5454
```rust
@@ -57,22 +57,22 @@ fn add_one(x: u32) -> u32 {
5757
}
5858
```
5959

60-
In the example above, `x` is a variable of type `u32`.
60+
In the example above, `x` is a variable of type `u32`.\
6161
The only difference between `x` and a variable declared with `let` is that functions arguments **must** have their type
62-
explicitly declared. The compiler won't infer it for you.
62+
explicitly declared. The compiler won't infer it for you.\
6363
This constraint allows the Rust compiler (and us humans!) to understand the function's signature without having to look
6464
at its implementation. That's a big boost for compilation speed[^speed]!
6565

6666
## Initialization
6767

68-
You don't have to initialize a variable when you declare it.
68+
You don't have to initialize a variable when you declare it.\
6969
For example
7070

7171
```rust
7272
let x: u32;
7373
```
7474

75-
is a valid variable declaration.
75+
is a valid variable declaration.\
7676
However, you must initialize the variable before using it. The compiler will throw an error if you don't:
7777

7878
```rust
@@ -101,4 +101,4 @@ help: consider assigning a value
101101

102102
- The exercise for this section is located in `exercises/02_basic_calculator/02_variables`
103103

104-
[^speed]: The Rust compiler needs all the help it can get when it comes to compilation speed.
104+
[^speed]: The Rust compiler needs all the help it can get when it comes to compilation speed.

0 commit comments

Comments
 (0)