Skip to content

Commit 3152086

Browse files
committed
Be more explicit about the scope of data in the example
1 parent fd20065 commit 3152086

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

Diff for: 09_01_shared_references.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ Well this has to do with how Rust handles memory management internally.
5252
This way, it's always clear which variable "owns" data so that when that variable goes out of scope, the memory associated with it can be automatically freed.
5353
This is different from how other languages handle memory with garbage collection or reference counting to know when memory should be freed.
5454

55-
So if we want to keep referring to the string after it has been passed to the `calculate_length` function, we need to use a *reference*.
55+
Since `s1` was moved to the scope of `calculate_length`, the `String` data will be deallocated when the function returns.
56+
That's why we can't print it when we are back in `main`, the data would not exist anymore.
57+
If we want to keep referring to the string after it has been passed to the `calculate_length` function, we need to pass a *reference* as argument instead.
5658
The reference will "borrow" the value and won't actually "own" the underlying data.
5759
This means that when the reference goes out of scope and is no longer in use, the heap data and its owner will still remain.
5860

0 commit comments

Comments
 (0)