Skip to content

Commit 2fc528a

Browse files
committed
---
yaml --- r: 52223 b: refs/heads/dist-snap c: f0fa67d h: refs/heads/master i: 52221: 658eb92 52219: c8b5c1b 52215: 9845623 52207: 124e9ef 52191: 09c4b89 52159: 7977215 52095: bd64783 51967: d089c10 51711: 775cbe8 51199: 3ac8141 v: v3
1 parent cf532c8 commit 2fc528a

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c
99
refs/heads/incoming: 44d4d6de762f3f9aae1fedcf454c66b79b3ad58d
10-
refs/heads/dist-snap: ca71c6ec5bb8106121cbe33eec5a6a9ca7786039
10+
refs/heads/dist-snap: f0fa67d6bf22bbca80c1c148de05c7990a2ed04b
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
1313
refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0

branches/dist-snap/doc/tutorial.md

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -863,11 +863,34 @@ allocating memory and indirecting through a pointer. But for big structs, or
863863
those with mutable fields, it can be useful to have a single copy on
864864
the stack or on the heap, and refer to that through a pointer.
865865

866-
Rust supports several types of pointers. The safe pointer types are
867-
`@T`, for managed boxes allocated on the local heap, `~T`, for
868-
uniquely-owned boxes allocated on the exchange heap, and `&T`, for
869-
borrowed pointers, which may point to any memory, and whose lifetimes
870-
are governed by the call stack.
866+
Whenever memory is allocated on the heap, the program needs a strategy to
867+
dispose of the memory when no longer needed. Most languages, such as Java or
868+
Python, use *garbage collection* for this, a strategy in which the program
869+
periodically searches for allocations that are no longer reachable in order
870+
to dispose of them. Other languages, such as C, use *manual memory
871+
management*, which relies on the programmer to specify when memory should be
872+
reclaimed.
873+
874+
Rust is in a different position. It differs from the garbage-collected
875+
environments in that allows the programmer to choose the disposal
876+
strategy on an object-by-object basis. Not only does this have benefits for
877+
performance, but we will later see that this model has benefits for
878+
concurrency as well, by making it possible for the Rust compiler to detect
879+
data races at compile time. Rust also differs from the manually managed
880+
languages in that it is *safe*—it uses a [pointer lifetime
881+
analysis][borrow] to ensure that manual memory management cannot cause memory
882+
errors at runtime.
883+
884+
[borrow]: tutorial-borrowed-ptr.html
885+
886+
The cornerstone of Rust's memory management is the concept of a *smart
887+
pointer*—a pointer type that indicates the lifetime of the object it points
888+
to. This solution is familiar to C++ programmers; Rust differs from C++,
889+
however, in that a small set of smart pointers are built into the language.
890+
The safe pointer types are `@T`, for *managed* boxes allocated on the *local
891+
heap*, `~T`, for *uniquely-owned* boxes allocated on the *exchange
892+
heap*, and `&T`, for *borrowed* pointers, which may point to any memory, and
893+
whose lifetimes are governed by the call stack.
871894

872895
All pointer types can be dereferenced with the `*` unary operator.
873896

@@ -919,7 +942,17 @@ node2.next = SomeNode(node3);
919942
node3.prev = SomeNode(node2);
920943
~~~
921944

922-
Managed boxes never cross task boundaries.
945+
Managed boxes never cross task boundaries. This has several benefits for
946+
performance:
947+
948+
* The Rust garbage collector does not need to stop multiple threads in order
949+
to collect garbage.
950+
951+
* You can separate your application into "real-time" tasks that do not use
952+
the garbage collector and "non-real-time" tasks that do, and the real-time
953+
tasks will not be interrupted by the non-real-time tasks.
954+
955+
C++ programmers will recognize `@T` as similar to `std::shared_ptr<T>`.
923956

924957
> ***Note:*** Currently, the Rust compiler generates code to reclaim
925958
> managed boxes through reference counting and a cycle collector, but
@@ -956,10 +989,19 @@ let z = *x + *y;
956989
assert z == 20;
957990
~~~~
958991

959-
Owned boxes, when they do not contain any managed boxes, can be sent
960-
to other tasks. The sending task will give up ownership of the box,
992+
When they do not contain any managed boxes, owned boxes can be sent
993+
to other tasks. The sending task will give up ownership of the box
961994
and won't be able to access it afterwards. The receiving task will
962-
become the sole owner of the box.
995+
become the sole owner of the box. This prevents *data races*—errors
996+
that could otherwise result from multiple tasks working on the same
997+
data without synchronization.
998+
999+
When an owned pointer goes out of scope or is overwritten, the object
1000+
it points to is immediately freed. Effective use of owned boxes can
1001+
therefore be an efficient alternative to garbage collection.
1002+
1003+
C++ programmers will recognize `~T` as similar to `std::unique_ptr<T>`
1004+
(or `std::auto_ptr<T>` in C++03 and below).
9631005

9641006
## Borrowed pointers
9651007

0 commit comments

Comments
 (0)