1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-06-17 14:47:35 +02:00

Format all Markdown files with dprint (#1157)

This is the result of running `dprint fmt` after removing `src/` from
the list of excluded directories.

This also reformats the Rust code: we might want to tweak this a bit in
the future since some of the changes removes the hand-formatting. Of
course, this formatting can be seen as a mis-feature, so maybe this is
good overall.

Thanks to mdbook-i18n-helpers 0.2, the POT file is nearly unchanged
after this, meaning that all existing translations remain valid! A few
messages were changed because of stray whitespace characters:

     msgid ""
     "Slices always borrow from another object. In this example, `a` has to remain "
    -"'alive' (in scope) for at least as long as our slice. "
    +"'alive' (in scope) for at least as long as our slice."
     msgstr ""

The formatting is enforced in CI and we will have to see how annoying
this is in practice for the many contributors. If it becomes annoying,
we should look into fixing dprint/check#11 so that `dprint` can annotate
the lines that need fixing directly, then I think we can consider more
strict formatting checks.

I added more customization to `rustfmt.toml`. This is to better emulate
the dense style used in the course:

- `max_width = 85` allows lines to take up the full width available in
our code blocks (when taking margins and the line numbers into account).
- `wrap_comments = true` ensures that we don't show very long comments
in the code examples. I edited some comments to shorten them and avoid
unnecessary line breaks — please trim other unnecessarily long comments
when you see them! Remember we're writing code for slides 😄
- `use_small_heuristics = "Max"` allows for things like struct literals
and if-statements to take up the full line width configured above.

The formatting settings apply to all our Rust code right now — I think
we could improve this with https://github.com/dprint/dprint/issues/711
which lets us add per-directory `dprint` configuration files. However,
the `inherit: true` setting is not yet implemented (as far as I can
tell), so a nested configuration file will have to copy most or all of
the top-level file.
This commit is contained in:
Martin Geisler
2023-12-31 00:15:07 +01:00
committed by GitHub
parent f43e72e0ad
commit c9f66fd425
302 changed files with 3067 additions and 2622 deletions

View File

@ -27,11 +27,11 @@ actually undefined, and might do different things on different platforms or
compilers. In Rust, it's defined.
Change the `i32`'s to `i16` to see an integer overflow, which panics (checked)
in a debug build and wraps in a release build. There are other options, such
as overflowing, saturating, and carrying. These are accessed with method
syntax, e.g., `(a * b).saturating_add(b * c).saturating_add(c * a)`.
in a debug build and wraps in a release build. There are other options, such as
overflowing, saturating, and carrying. These are accessed with method syntax,
e.g., `(a * b).saturating_add(b * c).saturating_add(c * a)`.
In fact, the compiler will detect overflow of constant expressions, which is
why the example requires a separate function.
In fact, the compiler will detect overflow of constant expressions, which is why
the example requires a separate function.
</details>

View File

@ -4,11 +4,11 @@ minutes: 30
# Exercise: Fibonacci
The first and second Fibonacci numbers are both `1`. For n>2, he n'th
Fibonacci number is calculated recursively as the sum of the n-1'th and n-2'th
Fibonacci numbers.
The first and second Fibonacci numbers are both `1`. For n>2, he n'th Fibonacci
number is calculated recursively as the sum of the n-1'th and n-2'th Fibonacci
numbers.
Write a function `fib(n)` that calculates the n'th Fibonacci number. When will
Write a function `fib(n)` that calculates the n'th Fibonacci number. When will
this function panic?
```rust,editable,should_panic

View File

@ -7,6 +7,7 @@ minutes: 5
Rust will look at how the variable is _used_ to determine the type:
<!-- mdbook-xgettext: skip -->
```rust,editable
fn takes_u32(x: u32) {
println!("u32: {x}");
@ -28,11 +29,13 @@ fn main() {
<details>
This slide demonstrates how the Rust compiler infers types based on constraints given by variable declarations and usages.
This slide demonstrates how the Rust compiler infers types based on constraints
given by variable declarations and usages.
It is very important to emphasize that variables declared like this are not of some sort of dynamic "any type" that can
hold any data. The machine code generated by such declaration is identical to the explicit declaration of a type.
The compiler does the job for us and helps us write more concise code.
It is very important to emphasize that variables declared like this are not of
some sort of dynamic "any type" that can hold any data. The machine code
generated by such declaration is identical to the explicit declaration of a
type. The compiler does the job for us and helps us write more concise code.
When nothing constrains the type of an integer literal, Rust defaults to `i32`.
This sometimes appears as `{integer}` in error messages. Similarly,

View File

@ -5,10 +5,10 @@ minutes: 10
# Strings
Rust has two types to represent strings, both of which will be covered in more
depth later. Both _always_ store UTF-8 encoded strings.
depth later. Both _always_ store UTF-8 encoded strings.
* `String` - a modifiable, owned string.
* `&str` - a read-only string. String literals have this type.
- `String` - a modifiable, owned string.
- `&str` - a read-only string. String literals have this type.
```rust,editable
fn main() {
@ -31,18 +31,19 @@ later, but this is enough for subsequent slides and exercises to use strings.
- Invalid UTF-8 in a string is UB, and this not allowed in safe Rust.
- `String` is a user-defined type with a constructor (`::new()`) and methods like `s.push_str(..)`.
- `String` is a user-defined type with a constructor (`::new()`) and methods
like `s.push_str(..)`.
- The `&` in `&str` indicates that this is a reference. We will cover references later, so for now
just think of `&str` as a unit meaning "a read-only string".
- The `&` in `&str` indicates that this is a reference. We will cover references
later, so for now just think of `&str` as a unit meaning "a read-only string".
- The commented-out line is indexing into the string by byte position. `12..13`
does not end on a character boundary, so the program panics. Adjust it to a
range that does, based on the error message.
- Raw strings allow you to create a `&str` value with escapes disabled: `r"\n"
== "\\\\n"`. You can embed double-quotes by using an equal amount of `#` on
either side of the quotes:
- Raw strings allow you to create a `&str` value with escapes disabled:
`r"\n" == "\\n"`. You can embed double-quotes by using an equal amount of `#`
on either side of the quotes:
<!-- mdbook-xgettext: skip -->
```rust,editable

View File

@ -4,10 +4,11 @@ minutes: 10
# Values
Here are some basic built-in types, and the syntax for literal values of each type.
Here are some basic built-in types, and the syntax for literal values of each
type.
| | Types | Literals |
|------------------------|--------------------------------------------|--------------------------------|
| ---------------------- | ------------------------------------------ | ------------------------------ |
| Signed integers | `i8`, `i16`, `i32`, `i64`, `i128`, `isize` | `-10`, `0`, `1_000`, `123_i64` |
| Unsigned integers | `u8`, `u16`, `u32`, `u64`, `u128`, `usize` | `0`, `123`, `10_u16` |
| Floating point numbers | `f32`, `f64` | `3.14`, `-10.0e20`, `2_f32` |
@ -16,16 +17,17 @@ Here are some basic built-in types, and the syntax for literal values of each ty
The types have widths as follows:
* `iN`, `uN`, and `fN` are _N_ bits wide,
* `isize` and `usize` are the width of a pointer,
* `char` is 32 bits wide,
* `bool` is 8 bits wide.
- `iN`, `uN`, and `fN` are _N_ bits wide,
- `isize` and `usize` are the width of a pointer,
- `char` is 32 bits wide,
- `bool` is 8 bits wide.
<details>
There are a few syntaxes which are not shown above:
- All underscores in numbers can be left out, they are for legibility only.
So `1_000` can be written as `1000` (or `10_00`), and `123_i64` can be written as `123i64`.
- All underscores in numbers can be left out, they are for legibility only. So
`1_000` can be written as `1000` (or `10_00`), and `123_i64` can be written as
`123i64`.
</details>

View File

@ -18,10 +18,10 @@ fn main() {
<details>
* Uncomment the `x = 20` to demonstrate that variables are immutable by default.
- Uncomment the `x = 20` to demonstrate that variables are immutable by default.
Add the `mut` keyword to allow changes.
* The `i32` here is the type of the variable. This must be known at compile
- The `i32` here is the type of the variable. This must be known at compile
time, but type inference (covered later) allows the programmer to omit it in
many cases.