Skip to content

Commit 1ac1260

Browse files
committed
---
yaml --- r: 42447 b: refs/heads/try c: 9c24c62 h: refs/heads/master i: 42445: 85a8091 42443: be30c08 42439: 7eacc93 42431: 05b16cd v: v3
1 parent 8c6413b commit 1ac1260

File tree

3 files changed

+68
-18
lines changed

3 files changed

+68
-18
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: 19dfec2aaf746535de1521f68421f9980dbf25de
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 2f46b763da2c098913884f101b6d71d69af41b49
5-
refs/heads/try: 46880337f497229e6c0ec0bf11d4af0e07c105e8
5+
refs/heads/try: 9c24c6221e8332f7c0ed015767a91f94b6e6b5eb
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: a810c03263670238bccd64cabb12a23a46e3a278

branches/try/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

branches/try/src/libstd/tempfile.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -19,18 +19,26 @@ use core::str;
1919

2020
pub fn mkdtemp(tmpdir: &Path, suffix: &str) -> Option<Path> {
2121
let r = rand::Rng();
22-
for 1000.times {
23-
let p = tmpdir.push(r.gen_str(16) + suffix);
24-
if os::make_dir(&p, 0x1c0) { // 700
22+
let mut i = 0u;
23+
while (i < 1000u) {
24+
let p = tmpdir.push(r.gen_str(16u) +
25+
str::from_slice(suffix));
26+
if os::make_dir(&p, 0x1c0i32) { // FIXME: u+rwx (#2349)
2527
return Some(p);
2628
}
29+
i += 1u;
2730
}
28-
None
31+
return None;
2932
}
3033

3134
#[test]
3235
fn test_mkdtemp() {
33-
let p = mkdtemp(&Path("."), "foobar").unwrap();
34-
os::remove_dir(&p);
35-
assert str::ends_with(p.to_str(), "foobar");
36+
let r = mkdtemp(&Path("."), "foobar");
37+
match r {
38+
Some(ref p) => {
39+
os::remove_dir(p);
40+
assert(str::ends_with(p.to_str(), "foobar"));
41+
}
42+
_ => assert(false)
43+
}
3644
}

0 commit comments

Comments
 (0)