-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a short discussion of assignment and ownership in ch. 04
Add a short new section showing how assignment to a mutable variable causes an existing *owned* binding to be freed immediately. Create a new code sample and a new diagram to illustrate the behavior. Fixes #4001.
- Loading branch information
1 parent
09239f3
commit 9eed938
Showing
12 changed files
with
432 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,55 @@ | ||
digraph { | ||
rankdir=LR; | ||
overlap=false; | ||
dpi=300.0; | ||
node [shape="plaintext"]; | ||
|
||
table0[label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"> | ||
<TR><TD COLSPAN="2" SIDES="B">s</TD></TR> | ||
<TR><TD>name</TD><TD>value</TD></TR> | ||
<TR><TD>ptr</TD><TD PORT="borrower"></TD></TR> | ||
</TABLE>>]; | ||
table1[label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"> | ||
<TR><TD COLSPAN="2" SIDES="B">s1</TD></TR> | ||
<TR><TD>name</TD><TD>value</TD></TR> | ||
<TR><TD PORT="borrowee">ptr</TD><TD PORT="pointer"></TD></TR> | ||
<TR><TD>len</TD><TD>5</TD></TR> | ||
<TR><TD>capacity</TD><TD>5</TD></TR> | ||
</TABLE>>]; | ||
table2[label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"> | ||
<TR><TD>index</TD><TD>value</TD></TR> | ||
<TR><TD PORT="pointee">0</TD><TD>h</TD></TR> | ||
<TR><TD>1</TD><TD>e</TD></TR> | ||
<TR><TD>2</TD><TD>l</TD></TR> | ||
<TR><TD>3</TD><TD>l</TD></TR> | ||
<TR><TD>4</TD><TD>o</TD></TR> | ||
</TABLE>>]; | ||
|
||
edge[tailclip="false"]; | ||
table1:pointer:c -> table2:pointee; | ||
table0:borrower:c -> table1:borrowee; | ||
} | ||
|
||
rankdir = LR; | ||
overlap = false; | ||
newrank = true; | ||
dpi = 300.0; | ||
splines = false; | ||
clusterrank = "local"; | ||
node [shape = "plaintext";]; | ||
|
||
subgraph cluster_stack { | ||
peripheries = 0; | ||
rank = "same"; | ||
|
||
// Just to have the right height! | ||
a [label = <<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" BGCOLOR="gray"> | ||
<TR><TD>a</TD></TR> | ||
<TR><TD>0</TD></TR> | ||
<TR><TD>1</TD></TR> | ||
<TR><TD>2</TD></TR> | ||
<TR><TD>3</TD></TR> | ||
</TABLE>>;style = invis;]; | ||
|
||
s [label = <<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"> | ||
<TR><TD COLSPAN="2" SIDES="B">s</TD></TR> | ||
<TR><TD>name</TD><TD>value</TD></TR> | ||
<TR><TD>ptr</TD><TD PORT="pointer"></TD></TR> | ||
<TR><TD>len</TD><TD>4</TD></TR> | ||
<TR><TD>capacity</TD><TD>4</TD></TR> | ||
</TABLE>>;]; | ||
} | ||
|
||
subgraph cluster_heap { | ||
peripheries = 0; | ||
rank = "same"; | ||
|
||
hello [label = <<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" BGCOLOR="gray"> | ||
<TR><TD>index</TD><TD>value</TD></TR> | ||
<TR><TD PORT="pointee">0</TD><TD>h</TD></TR> | ||
<TR><TD>1</TD><TD>e</TD></TR> | ||
<TR><TD>2</TD><TD>l</TD></TR> | ||
<TR><TD>3</TD><TD>l</TD></TR> | ||
<TR><TD>4</TD><TD>o</TD></TR> | ||
</TABLE>>;]; | ||
|
||
ahoy [label = <<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"> | ||
<TR><TD>index</TD><TD>value</TD></TR> | ||
<TR><TD PORT="pointee">0</TD><TD>a</TD></TR> | ||
<TR><TD>1</TD><TD>h</TD></TR> | ||
<TR><TD>2</TD><TD>o</TD></TR> | ||
<TR><TD>3</TD><TD>y</TD></TR> | ||
</TABLE>>;]; | ||
} | ||
|
||
s -> ahoy [tailport = "pointer:c"; headport = "pointee"; tailclip = false;]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
digraph { | ||
rankdir=LR; | ||
overlap=false; | ||
dpi=300.0; | ||
node [shape="plaintext"]; | ||
|
||
table0[label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"> | ||
<TR><TD COLSPAN="2" SIDES="B">world</TD></TR> | ||
<TR><TD>name</TD><TD>value</TD></TR> | ||
<TR><TD>ptr</TD><TD PORT="pointer2"></TD></TR> | ||
<TR><TD>len</TD><TD>5</TD></TR> | ||
</TABLE>>]; | ||
|
||
table3[label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"> | ||
<TR><TD COLSPAN="2" SIDES="B">s</TD></TR> | ||
<TR><TD>name</TD><TD>value</TD></TR> | ||
<TR><TD>ptr</TD><TD PORT="pointer"></TD></TR> | ||
<TR><TD>len</TD><TD>11</TD></TR> | ||
<TR><TD>capacity</TD><TD>11</TD></TR> | ||
</TABLE>>]; | ||
table4[label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"> | ||
<TR><TD>index</TD><TD>value</TD></TR> | ||
<TR><TD PORT="pointee">0</TD><TD>h</TD></TR> | ||
<TR><TD>1</TD><TD>e</TD></TR> | ||
<TR><TD>2</TD><TD>l</TD></TR> | ||
<TR><TD>3</TD><TD>l</TD></TR> | ||
<TR><TD>4</TD><TD>o</TD></TR> | ||
<TR><TD>5</TD><TD> </TD></TR> | ||
<TR><TD PORT="pointee2">6</TD><TD>w</TD></TR> | ||
<TR><TD>7</TD><TD>o</TD></TR> | ||
<TR><TD>8</TD><TD>r</TD></TR> | ||
<TR><TD>9</TD><TD>l</TD></TR> | ||
<TR><TD>10</TD><TD>d</TD></TR> | ||
</TABLE>>]; | ||
|
||
|
||
edge[tailclip="false"]; | ||
table0:pointer2:c -> table4:pointee2; | ||
table3:pointer:c -> table4:pointee; | ||
} | ||
|
6 changes: 6 additions & 0 deletions
6
listings/ch04-understanding-ownership/no-listing-04b-replacement-drop/Cargo.lock
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
listings/ch04-understanding-ownership/no-listing-04b-replacement-drop/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "ownership" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
8 changes: 8 additions & 0 deletions
8
listings/ch04-understanding-ownership/no-listing-04b-replacement-drop/src/main.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
fn main() { | ||
// ANCHOR: here | ||
let mut s = String::from("hello"); | ||
s = String::from("ahoy"); | ||
|
||
println!("{s}, world!"); | ||
// ANCHOR_END: here | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.