Skip to content

Commit

Permalink
Closes #216
Browse files Browse the repository at this point in the history
  • Loading branch information
playbahn committed Mar 27, 2024
1 parent ed022fc commit 9dfb17e
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/04_pinning/01_chapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,21 +237,21 @@ fn main() {
# }
```

Naively, we could think that what we should get a debug print of `test1` two times like this:
Naively, we could think that what we should get a debug print of `test2` before and after the swap like this:

```rust, ignore
a: test1, b: test1
a: test2, b: test2
a: test1, b: test1
```

But instead we get:

```rust, ignore
a: test1, b: test1
a: test2, b: test2
a: test1, b: test2
```

The pointer to `test2.b` still points to the old location which is inside `test1`
The pointer `test2.b` still points to the old location which is inside `test1`
now. The struct is not self-referential anymore, it holds a pointer to a field
in a different object. That means we can't rely on the lifetime of `test2.b` to
be tied to the lifetime of `test2` anymore.
Expand All @@ -265,10 +265,15 @@ fn main() {
let mut test2 = Test::new("test2");
test2.init();

println!("a: {}, b: {}", test1.a(), test1.b());
println!("test1: a: {}, b: {}", test1.a(), test1.b());
println!("test2: a: {}, b: {}", test2.a(), test2.b());
std::mem::swap(&mut test1, &mut test2);
println!("----------swap-----------");
println!("test1: a: {}, b: {}", test1.a(), test1.b());
println!("test2: a: {}, b: {}", test2.a(), test2.b());
println!("`test1.a = \"I've totally changed now!\".to_string();`");
test1.a = "I've totally changed now!".to_string();
println!("a: {}, b: {}", test2.a(), test2.b());
println!("test2: a: {}, b: {}", test2.a(), test2.b());

}
# #[derive(Debug)]
Expand Down

0 comments on commit 9dfb17e

Please sign in to comment.