You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-12-22 22:51:12 +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:
@@ -8,6 +8,7 @@ Exclusive references, also known as mutable references, allow changing the value
|
||||
they refer to. They have type `&mut T`.
|
||||
|
||||
<!-- mdbook-xgettext: skip -->
|
||||
|
||||
```rust,editable
|
||||
fn main() {
|
||||
let mut point = (1, 2);
|
||||
@@ -21,14 +22,14 @@ fn main() {
|
||||
|
||||
Key points:
|
||||
|
||||
* "Exclusive" means that only this reference can be used to access the value.
|
||||
No other references (shared or exclusive) can exist at the same time, and the
|
||||
- "Exclusive" means that only this reference can be used to access the value. No
|
||||
other references (shared or exclusive) can exist at the same time, and the
|
||||
referenced value cannot be accessed while the exclusive reference exists. Try
|
||||
making an `&point.0` or changing `point.0` while `x_coord` is alive.
|
||||
|
||||
* Be sure to note the difference between `let mut x_coord: &i32` and `let
|
||||
x_coord: &mut i32`. The first one represents a shared reference which can be
|
||||
bound to different values, while the second represents an exclusive reference
|
||||
to a mutable value.
|
||||
- Be sure to note the difference between `let mut x_coord: &i32` and
|
||||
`let x_coord: &mut i32`. The first one represents a shared reference which can
|
||||
be bound to different values, while the second represents an exclusive
|
||||
reference to a mutable value.
|
||||
|
||||
</details>
|
||||
|
||||
@@ -32,10 +32,7 @@ fn normalize(vector: &mut [f64; 3]) {
|
||||
|
||||
// ANCHOR: main
|
||||
fn main() {
|
||||
println!(
|
||||
"Magnitude of a unit vector: {}",
|
||||
magnitude(&[0.0, 1.0, 0.0])
|
||||
);
|
||||
println!("Magnitude of a unit vector: {}", magnitude(&[0.0, 1.0, 0.0]));
|
||||
|
||||
let mut v = [1.0, 2.0, 9.0];
|
||||
println!("Magnitude of {v:?}: {}", magnitude(&v));
|
||||
|
||||
@@ -9,6 +9,7 @@ for the value, and is also called "borrowing". Shared references are read-only,
|
||||
and the referenced data cannot change.
|
||||
|
||||
<!-- mdbook-xgettext: skip -->
|
||||
|
||||
```rust,editable
|
||||
fn main() {
|
||||
let a = 'A';
|
||||
@@ -20,12 +21,14 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
A shared reference to a type `T` has type `&T`. A reference value is made with the `&`
|
||||
operator. The `*` operator "dereferences" a reference, yielding its value.
|
||||
A shared reference to a type `T` has type `&T`. A reference value is made with
|
||||
the `&` operator. The `*` operator "dereferences" a reference, yielding its
|
||||
value.
|
||||
|
||||
Rust will statically forbid dangling references:
|
||||
|
||||
<!-- mdbook-xgettext: skip -->
|
||||
|
||||
```rust,editable,compile_fail
|
||||
fn x_axis(x: i32) -> &(i32, i32) {
|
||||
let point = (x, 0);
|
||||
@@ -35,36 +38,34 @@ fn x_axis(x: i32) -> &(i32, i32) {
|
||||
|
||||
<details>
|
||||
|
||||
* A reference is said to "borrow" the value it refers to, and this is a good
|
||||
- A reference is said to "borrow" the value it refers to, and this is a good
|
||||
model for students not familiar with pointers: code can use the reference to
|
||||
access the value, but is still "owned" by the original variable. The course
|
||||
will get into more detail on ownership in day 3.
|
||||
|
||||
* References are implemented as pointers, and a key advantage is that they can
|
||||
- References are implemented as pointers, and a key advantage is that they can
|
||||
be much smaller than the thing they point to. Students familiar with C or C++
|
||||
will recognize references as pointers. Later parts of the course will cover
|
||||
how Rust prevents the memory-safety bugs that come from using raw pointers.
|
||||
|
||||
* Rust does not automatically create references for you - the `&` is always
|
||||
- Rust does not automatically create references for you - the `&` is always
|
||||
required.
|
||||
|
||||
* Rust will auto-dereference in some cases, in particular when invoking
|
||||
methods (try `r.count_ones()`). There is no need for an `->` operator
|
||||
like in C++.
|
||||
- Rust will auto-dereference in some cases, in particular when invoking methods
|
||||
(try `r.count_ones()`). There is no need for an `->` operator like in C++.
|
||||
|
||||
* In this example, `r` is mutable so that it can be reassigned (`r = &b`).
|
||||
Note that this re-binds `r`, so that it refers to something else. This is
|
||||
different from C++, where assignment to a reference changes the referenced
|
||||
value.
|
||||
- In this example, `r` is mutable so that it can be reassigned (`r = &b`). Note
|
||||
that this re-binds `r`, so that it refers to something else. This is different
|
||||
from C++, where assignment to a reference changes the referenced value.
|
||||
|
||||
* A shared reference does not allow modifying the value it refers to, even if
|
||||
- A shared reference does not allow modifying the value it refers to, even if
|
||||
that value was mutable. Try `*r = 'X'`.
|
||||
|
||||
* Rust is tracking the lifetimes of all references to ensure they live long
|
||||
enough. Dangling references cannot occur in safe Rust. `x_axis` would return
|
||||
a reference to `point`, but `point` will be deallocated when the function
|
||||
- Rust is tracking the lifetimes of all references to ensure they live long
|
||||
enough. Dangling references cannot occur in safe Rust. `x_axis` would return a
|
||||
reference to `point`, but `point` will be deallocated when the function
|
||||
returns, so this will not compile.
|
||||
|
||||
* We will talk more about borrowing when we get to ownership.
|
||||
- We will talk more about borrowing when we get to ownership.
|
||||
|
||||
</details>
|
||||
|
||||
Reference in New Issue
Block a user