Skip to content

Commit

Permalink
Minor updates based on instruction (#1583)
Browse files Browse the repository at this point in the history
This contains some minor updates from #1565.

---------

Co-authored-by: Marshall Pierce <575695+marshallpierce@users.noreply.github.com>
  • Loading branch information
djmitche and marshallpierce committed Dec 13, 2023
1 parent 35442ad commit 9563f05
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
2 changes: 2 additions & 0 deletions src/memory-management/exercise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ struct Package {
impl Package {
// ANCHOR_END: Package
// ANCHOR: as_dependency
/// Return a representation of this package as a dependency, for use in
/// building other packages.
fn as_dependency(&self) -> Dependency {
// ANCHOR_END: as_dependency
Dependency {
Expand Down
8 changes: 4 additions & 4 deletions src/slices-and-lifetimes/lifetime-annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
minutes: 10
---

# Lifetimes
# Lifetime Annotations

A reference has a _lifetime_, which must "outlive" the value it refers to. This
is verified by the borrow checker.
A reference has a _lifetime_, which must not "outlive" the value it refers to.
This is verified by the borrow checker.

The lifetime can be implicit - this is what we have seen so far. Lifetimes can
also be explicit: `&'a Point`, `&'document str`. Lifetimes start with `'` and
Expand All @@ -19,7 +19,7 @@ ambiguity; the compiler verifies that there is a valid solution.
Lifetimes become more complicated when considering passing values to and
returning values from functions.

```rust,compile_fail
```rust,eitable,compile_fail
#[derive(Debug)]
struct Point(i32, i32);
Expand Down
46 changes: 24 additions & 22 deletions src/smart-pointers/box.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,29 @@ Recursive data types or data types with dynamic sizes need to use a `Box`:
```rust,editable
#[derive(Debug)]
enum List<T> {
Cons(T, Box<List<T>>),
/// A non-empty list, consisting of the first element and the rest of the list.
Element(T, Box<List<T>>),
/// An empty list.
Nil,
}
fn main() {
let list: List<i32> = List::Cons(1, Box::new(List::Cons(2, Box::new(List::Nil))));
let list: List<i32> = List::Element(1, Box::new(List::Element(2, Box::new(List::Nil))));
println!("{list:?}");
}
```

```bob
Stack Heap
.- - - - - - - - - - - - -. .- - - - - - - - - - - - - - - - - - - - - - - -.
: : : :
: list : : :
: +------+----+----+ : : +------+----+----+ +------+----+----+ :
: | Cons | 1 | o--+----+-----+--->| Cons | 2 | o--+--->| Nil | // | // | :
: +------+----+----+ : : +------+----+----+ +------+----+----+ :
: : : :
: : : :
'- - - - - - - - - - - - -' '- - - - - - - - - - - - - - - - - - - - - - - -'
.- - - - - - - - - - - - - - . .- - - - - - - - - - - - - - - - - - - - - - - - -.
: : : :
: list : : :
: +---------+----+----+ : : +---------+----+----+ +------+----+----+ :
: | Element | 1 | o--+----+-----+--->| Element | 2 | o--+--->| Nil | // | // | :
: +---------+----+----+ : : +---------+----+----+ +------+----+----+ :
: : : :
: : : :
'- - - - - - - - - - - - - - ' '- - - - - - - - - - - - - - - - - - - - - - - - -'
```
<details>

Expand All @@ -79,12 +81,12 @@ element of the `List` in the heap.
```rust,editable
#[derive(Debug)]
enum List<T> {
Cons(T, Box<List<T>>),
Element(T, Box<List<T>>),
Nil,
}
fn main() {
let list: List<i32> = List::Cons(1, Box::new(List::Cons(2, Box::new(List::Nil))));
let list: List<i32> = List::Element(1, Box::new(List::Element(2, Box::new(List::Nil))));
println!("{list:?}");
}
```
Expand All @@ -94,15 +96,15 @@ allows the compiler to optimize the memory layout:

```bob
Stack Heap
.- - - - - - - - - - - - -. .- - - - - - - - - - - - - - - - - - - - - - -.
: : : :
: list : : :
: +----+----+ : : +----+----+ +----+------+ :
: | 1 | o--+-----------+-----+--->| 2 | o--+--->| // | null | :
: +----+----+ : : +----+----+ +----+------+ :
: : : :
: : : :
`- - - - - - - - - - - - -' '- - - - - - - - - - - - - - - - - - - - - - -'
.- - - - - - - - - - - - - - . .- - - - - - - - - - - - - -.
: : : :
: list : : :
: +---------+----+----+ : : +---------+----+----+ :
: | Element | 1 | o--+----+-----+--->| Element | 2 | // | :
: +---------+----+----+ : : +---------+----+----+ :
: : : :
: : : :
'- - - - - - - - - - - - - - ' '- - - - - - - - - - - - - -'
```

</details>

0 comments on commit 9563f05

Please sign in to comment.