Fix rust-doc error
There was a partial rust code block in the readme that was invalid because of a missing line. I inlined the code snippet into the text to fix the error. This also improves readability a bit.
This commit is contained in:
parent
fc02eee8f6
commit
4f194a76c8
1 changed files with 11 additions and 16 deletions
|
@ -25,35 +25,30 @@ where
|
||||||
|
|
||||||
Why doesn't this code compile? It helps to look at the lifetime bounds that
|
Why doesn't this code compile? It helps to look at the lifetime bounds that
|
||||||
the compiler is automatically adding ("Lifetime Ellision", Chapter 10.3 in the
|
the compiler is automatically adding ("Lifetime Ellision", Chapter 10.3 in the
|
||||||
Rust book) to the `nested_borrow_mut` and `borrow_mut` functions. In both cases
|
Rust book) to the `nested_borrow_mut()` and `borrow_mut()` functions. In both
|
||||||
the input is a reference to `self`, so the compiler attempts to assign the
|
cases the input is a reference to `self`, so the compiler attempts to assign
|
||||||
the same lifetime to the input and output.
|
the same lifetime to the input and output.
|
||||||
|
|
||||||
Looking specifically at `nested_borrow_mut`, we see that there are three object
|
Looking specifically at `nested_borrow_mut()`, we see that there are three
|
||||||
references to keep track of, along with their associated lifetimes:
|
object references to keep track of, along with their associated lifetimes:
|
||||||
- `self` (which is a `&mut T`)
|
- `self` (which is a `&mut T`)
|
||||||
- `u_ref` (which is a `&mut U`)
|
- `u_ref` (which is a `&mut U`)
|
||||||
- `v_ref` (which is a `&mut V`)
|
- `v_ref` (which is a `&mut V`)
|
||||||
|
|
||||||
The `borrow_mut()` method implicitly requires that that the input and output
|
The `borrow_mut()` method implicitly requires that that the input and output
|
||||||
have the same lifetime bounds. Thus the lines:
|
have the same lifetime bounds. Thus the lines: `let u_ref = self.borrow_mut();`
|
||||||
|
and `let v_ref = u_ref.borrow_mut();` in `nested_borrow_mut()` above imply that
|
||||||
```rust
|
`u_ref` and `self` must share a lifetime bound, and also that `v_ref` and
|
||||||
let u_ref = self.borrow_mut();
|
`u_ref` share a lifetime bound. The problem is that the function signature for
|
||||||
let v_ref = u_ref.borrow_mut();
|
`nested_borrow_mut()` only gives the compiler information about the lifetimes
|
||||||
```
|
of `self` and `v_ref` -- nothing about `u_ref`.
|
||||||
|
|
||||||
imply that `u_ref` and `self` must share a lifetime bound, and also that
|
|
||||||
`v_ref` and `u_ref` share a lifetime bound. The problem is that the function
|
|
||||||
signature for `nested_borrow_mut` only gives the compiler information about the
|
|
||||||
lifetimes of `self` and `v_ref` -- nothing about `u_ref`.
|
|
||||||
|
|
||||||
The way to fix this error is then to explicitly tell the compiler that the
|
The way to fix this error is then to explicitly tell the compiler that the
|
||||||
lifetime of `u_ref` is the same as `self` and `v_ref`, which then allows it
|
lifetime of `u_ref` is the same as `self` and `v_ref`, which then allows it
|
||||||
to satisfy the two lifetime bound requirements described above.
|
to satisfy the two lifetime bound requirements described above.
|
||||||
|
|
||||||
Here is the working version of the code:
|
Here is the working version of the code:
|
||||||
```rust
|
```
|
||||||
use std::borrow::BorrowMut;
|
use std::borrow::BorrowMut;
|
||||||
|
|
||||||
trait NestedBorrowMut<'a, U, V> {
|
trait NestedBorrowMut<'a, U, V> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue