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

Closes #216 #217

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading