You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 09_01_shared_references.md
+3-2
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ A mutable reference can modify the underlying data where as the shared reference
5
5
We already saw an example of a shared reference in chapter 6 where we created a slice reference to data on the heap by prepending `[u8]` with the `&` symbol.
6
6
7
7
A reference is a kind of pointer.
8
-
It points to some data elsewhere and "borrows" it instead of "owns" it.
8
+
It points to some data elsewhere and "borrows" it instead of "owning" it.
9
9
What does this mean?
10
10
Well let's see with an [example](https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#references-and-borrowing):
11
11
@@ -46,6 +46,7 @@ It also mentions that the type, `String` does not implement the `Copy` trait.
46
46
By default, any type that does not implement the `Copy` trait will get "moved" when passed as an argument.
47
47
This means the variable that contains it will no longer be available within the scope of original function.
48
48
It's "ownership" is now passed to the function being called, which in this case is `calculate_length`.
49
+
49
50
Why does Rust do this?
50
51
Well this has to do with how Rust handles memory management internally.
51
52
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.
@@ -55,7 +56,7 @@ So if we want to keep referring to the string after it has been passed to the `c
55
56
The reference will "borrow" the value and won't actually "own" the underlying data.
56
57
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.
57
58
58
-
We can create a reference by placing the `&` symbol in front and modifying the function argument type:
59
+
We can create a reference by placing the `&` symbol in front of an identifier and modifying the function argument type:
0 commit comments