Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miscellaneous minor improvements #2370

Merged
merged 12 commits into from
Sep 20, 2024
12 changes: 12 additions & 0 deletions src/control-flow-basics/break-continue/labels.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,15 @@ fn main() {
print!("elements searched: {elements_searched}");
}
```

<details>

- Labeled break also works on arbitrary blocks, e.g.
```rust
'label: {
break 'label;
println!("This line gets skipped");
}
```

</details>
randomPoison marked this conversation as resolved.
Show resolved Hide resolved
18 changes: 6 additions & 12 deletions src/methods-and-traits/exercise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,18 @@

// ANCHOR: solution
// ANCHOR: setup
use std::fmt::Display;

pub trait Logger {
/// Log a message at the given verbosity level.
fn log(&self, verbosity: u8, message: impl Display);
randomPoison marked this conversation as resolved.
Show resolved Hide resolved
fn log(&self, verbosity: u8, message: &str);
}

struct StderrLogger;

impl Logger for StderrLogger {
fn log(&self, verbosity: u8, message: impl Display) {
fn log(&self, verbosity: u8, message: &str) {
eprintln!("verbosity={verbosity}: {message}");
}
}

fn do_things(logger: &impl Logger) {
logger.log(5, "FYI");
logger.log(2, "Uhoh");
}
// ANCHOR_END: setup

/// Only log messages up to the given verbosity level.
Expand All @@ -42,7 +35,7 @@ struct VerbosityFilter {
}

impl Logger for VerbosityFilter {
fn log(&self, verbosity: u8, message: impl Display) {
fn log(&self, verbosity: u8, message: &str) {
if verbosity <= self.max_verbosity {
self.inner.log(verbosity, message);
}
Expand All @@ -51,7 +44,8 @@ impl Logger for VerbosityFilter {

// ANCHOR: main
fn main() {
let l = VerbosityFilter { max_verbosity: 3, inner: StderrLogger };
do_things(&l);
let logger = VerbosityFilter { max_verbosity: 3, inner: StderrLogger };
logger.log(5, "FYI");
logger.log(2, "Uhoh");
}
// ANCHOR_END: main
7 changes: 0 additions & 7 deletions src/pattern-matching/destructuring-enums.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,5 @@ arm, `half` is bound to the value inside the `Ok` variant. In the second arm,
matched.
- Demonstrate what happens when the search is inexhaustive. Note the advantage
the Rust compiler provides by confirming when all cases are handled.
- Save the result of `divide_in_two` in the `result` variable and `match` it in
a loop. That won't compile because `msg` is consumed when matched. To fix it,
match `&result` instead of `result`. That will make `msg` a reference so it
won't be consumed. This
["match ergonomics"](https://rust-lang.github.io/rfcs/2005-match-ergonomics.html)
appeared in Rust 2018. If you want to support older Rust, replace `msg` with
`ref msg` in the pattern.

</details>
4 changes: 2 additions & 2 deletions src/pattern-matching/exercise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ fn eval(e: Expression) -> Result<i64, String> {
Expression::Op { op, left, right } => {
let left = match eval(*left) {
Ok(v) => v,
e @ Err(_) => return e,
Err(e) => return Err(e),
};
let right = match eval(*right) {
Ok(v) => v,
e @ Err(_) => return e,
Err(e) => return Err(e),
randomPoison marked this conversation as resolved.
Show resolved Hide resolved
};
Ok(match op {
Operation::Add => left + right,
Expand Down
7 changes: 0 additions & 7 deletions src/references/slices.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ fn main() {
```

- Slices borrow data from the sliced type.
- Question: What happens if you modify `a[3]` right before printing `s`?

<details>

Expand All @@ -43,10 +42,4 @@ fn main() {
- Slices always borrow from another object. In this example, `a` has to remain
'alive' (in scope) for at least as long as our slice.

- The question about modifying `a[3]` can spark an interesting discussion, but
the answer is that for memory safety reasons you cannot do it through `a` at
this point in the execution, but you can read the data from both `a` and `s`
safely. It works before you created the slice, and again after the `println`,
when the slice is no longer used.

</details>