Skip to content

Commit 476725b

Browse files
committed
---
yaml --- r: 24454 b: refs/heads/master c: 84bc74e h: refs/heads/master v: v3
1 parent 495b8d6 commit 476725b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+416
-503
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 937f8f4067836de1a56dbe75fb8b1ae3179a73e9
2+
refs/heads/master: 84bc74e54a2d46f87c4205b99e78c2ce3ef9c881
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: cd6f24f9d14ac90d167386a56e7a6ac1f0318195
55
refs/heads/try: ffbe0e0e00374358b789b0037bcb3a577cd218be

trunk/doc/rust.md

Lines changed: 141 additions & 130 deletions
Large diffs are not rendered by default.

trunk/doc/tutorial.md

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,7 @@ and functions defined in it. If it has a `main` function, it can be
162162
compiled to an executable. Rust does not allow code that's not a
163163
declaration to appear at the top level of the file—all statements must
164164
live inside a function. Rust programs can also be compiled as
165-
libraries, and included in other programs. The `extern mod std`
166-
directive that appears at the top of many examples imports the
167-
[standard library][std], described in more detail [later
168-
on](#modules-and-crates).
169-
170-
[std]: http://doc.rust-lang.org/doc/std
165+
libraries, and included in other programs.
171166

172167
## Editing Rust code
173168

@@ -1284,6 +1279,73 @@ For a more in-depth explanation of borrowed pointers, read the
12841279
12851280
[borrowtut]: tutorial-borrowed-ptr.html
12861281
1282+
## Dereferencing pointers
1283+
1284+
Rust uses the unary star operator (`*`) to access the contents of a
1285+
box or pointer, similarly to C.
1286+
1287+
~~~
1288+
let managed = @10;
1289+
let owned = ~20;
1290+
let borrowed = &30;
1291+
1292+
let sum = *managed + *owned + *borrowed;
1293+
~~~
1294+
1295+
Dereferenced mutable pointers may appear on the left hand side of
1296+
assignments, in which case the value they point to is modified.
1297+
1298+
~~~
1299+
let managed = @mut 10;
1300+
let owned = ~mut 20;
1301+
1302+
let mut value = 30;
1303+
let borrowed = &mut value;
1304+
1305+
*managed = *owned + 10;
1306+
*owned = *borrowed + 100;
1307+
*borrowed = *managed + 1000;
1308+
~~~
1309+
1310+
Pointers have high operator precedence, but lower precedence than the
1311+
dot operator used for field and method access. This can lead to some
1312+
awkward code filled with parenthesis.
1313+
1314+
~~~
1315+
# struct Point { x: float, y: float }
1316+
# enum Shape { Rectangle(Point, Point) }
1317+
# impl Shape { fn area() -> int { 0 } }
1318+
let start = @Point { x: 10f, y: 20f };
1319+
let end = ~Point { x: (*start).x + 100f, y: (*start).y + 100f };
1320+
let rect = &Rectangle(*start, *end);
1321+
let area = (*rect).area();
1322+
~~~
1323+
1324+
To combat this ugliness the dot operator performs _automatic pointer
1325+
dereferencing_ on the receiver (the value on the left hand side of the
1326+
dot), so in most cases dereferencing the receiver is not necessary.
1327+
1328+
~~~
1329+
# struct Point { x: float, y: float }
1330+
# enum Shape { Rectangle(Point, Point) }
1331+
# impl Shape { fn area() -> int { 0 } }
1332+
let start = @Point { x: 10f, y: 20f };
1333+
let end = ~Point { x: start.x + 100f, y: start.y + 100f };
1334+
let rect = &Rectangle(*start, *end);
1335+
let area = rect.area();
1336+
~~~
1337+
1338+
Auto-dereferencing is performed through any number of pointers. If you
1339+
felt inclined you could write something silly like
1340+
1341+
~~~
1342+
# struct Point { x: float, y: float }
1343+
let point = &@~Point { x: 10f, y: 20f };
1344+
io::println(fmt!("%f", point.x));
1345+
~~~
1346+
1347+
The indexing operator (`[]`) is also auto-dereferencing.
1348+
12871349
# Vectors and strings
12881350
12891351
Vectors are a contiguous section of memory containing zero or more
@@ -1381,7 +1443,7 @@ mutable_crayons[0] = Apricot;
13811443
This is a simple example of Rust's _dual-mode data structures_, also
13821444
referred to as _freezing and thawing_.
13831445

1384-
Strings are implemented with vectors of `[u8]`, though they have a distinct
1446+
Strings are implemented with vectors of `u8`, though they have a distinct
13851447
type. They support most of the same allocation options as
13861448
vectors, though the string literal without a storage sigil, e.g.
13871449
`"foo"` is treated differently than a comparable vector (`[foo]`).
@@ -1491,7 +1553,7 @@ access local variables in the enclosing scope.
14911553

14921554
~~~~
14931555
let mut max = 0;
1494-
(~[1, 2, 3]).map(|x| if *x > max { max = *x });
1556+
[1, 2, 3].map(|x| if *x > max { max = *x });
14951557
~~~~
14961558

14971559
Stack closures are very efficient because their environment is
@@ -1519,8 +1581,7 @@ This code creates a closure that adds a given string to its argument,
15191581
returns it from a function, and then calls it:
15201582

15211583
~~~~
1522-
extern mod std;
1523-
1584+
# extern mod std;
15241585
fn mk_appender(suffix: ~str) -> fn@(~str) -> ~str {
15251586
return fn@(s: ~str) -> ~str { s + suffix };
15261587
}

trunk/src/etc/kate/rust.xml

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE language SYSTEM "language.dtd">
3-
<language name="Rust" version="0.3.1" kateversion="2.4" section="Sources" extensions="*.rs;*.rc" mimetype="text/x-rust" priority="15">
3+
<language name="Rust" version="0.4.0" kateversion="2.4" section="Sources" extensions="*.rs;*.rc" mimetype="text/x-rust" priority="15">
44
<highlighting>
55
<list name="fn">
66
<item> fn </item>
@@ -9,59 +9,39 @@
99
<item> type </item>
1010
</list>
1111
<list name="keywords">
12-
<item> alt </item>
13-
<item> again </item>
1412
<item> as </item>
1513
<item> assert </item>
1614
<item> break </item>
17-
<item> check </item>
18-
<item> claim </item>
1915
<item> const </item>
2016
<item> copy </item>
2117
<item> do </item>
2218
<item> drop </item>
2319
<item> else </item>
20+
<item> enum </item>
2421
<item> export </item>
2522
<item> extern </item>
26-
<item> f16 </item>
27-
<item> f80 </item>
28-
<item> f128 </item>
2923
<item> fail </item>
3024
<item> for </item>
3125
<item> if </item>
3226
<item> impl </item>
33-
<item> import </item>
34-
<item> in </item>
3527
<item> let </item>
3628
<item> log </item>
3729
<item> loop </item>
38-
<item> m32 </item>
39-
<item> m64 </item>
40-
<item> m128 </item>
4130
<item> match </item>
4231
<item> mod </item>
43-
<item> module </item>
4432
<item> move </item>
4533
<item> mut </item>
46-
<item> new </item>
47-
<item> of </item>
48-
<item> owned </item>
4934
<item> priv </item>
5035
<item> pub </item>
5136
<item> pure </item>
52-
<item> ret </item>
37+
<item> ref </item>
5338
<item> return </item>
54-
<item> to </item>
55-
<item> unchecked </item>
39+
<item> static </item>
40+
<item> struct </item>
41+
<item> trait </item>
5642
<item> unsafe </item>
5743
<item> use </item>
5844
<item> while </item>
59-
<item> with </item>
60-
<item> mod </item>
61-
<item> trait </item>
62-
<item> class </item>
63-
<item> struct </item>
64-
<item> enum </item>
6545
</list>
6646
<list name="types">
6747
<item> bool </item>
@@ -80,8 +60,9 @@
8060
<item> float </item>
8161
<item> char </item>
8262
<item> str </item>
83-
<item> option </item>
84-
<item> either </item>
63+
<item> Either </item>
64+
<item> Option </item>
65+
<item> Result </item>
8566
</list>
8667
<list name="ctypes">
8768
<item> c_float </item>
@@ -121,16 +102,16 @@
121102
<list name="constants">
122103
<item> true </item>
123104
<item> false </item>
124-
<item> some </item>
125-
<item> none </item>
126-
<item> left </item>
127-
<item> right </item>
128-
<item> ok </item>
129-
<item> err </item>
130-
<item> success </item>
131-
<item> failure </item>
132-
<item> cons </item>
133-
<item> nil </item>
105+
<item> Some </item>
106+
<item> None </item>
107+
<item> Left </item>
108+
<item> Right </item>
109+
<item> Ok </item>
110+
<item> Err </item>
111+
<item> Success </item>
112+
<item> Failure </item>
113+
<item> Cons </item>
114+
<item> Nil </item>
134115
</list>
135116
<list name="cconstants">
136117
<item> EXIT_FAILURE </item>

trunk/src/libcore/at_vec.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ extern mod rustrt {
2121
#[abi = "rust-intrinsic"]
2222
extern mod rusti {
2323
#[legacy_exports];
24-
#[cfg(stage0)]
25-
fn move_val_init<T>(&dst: T, -src: T);
26-
#[cfg(stage1)]
27-
#[cfg(stage2)]
2824
fn move_val_init<T>(dst: &mut T, -src: T);
2925
}
3026

@@ -181,20 +177,6 @@ pub mod raw {
181177
}
182178
}
183179

184-
// This doesn't bother to make sure we have space.
185-
#[cfg(stage0)]
186-
#[inline(always)] // really pretty please
187-
pub unsafe fn push_fast<T>(v: &mut @[const T], initval: T) {
188-
let repr: **VecRepr = ::cast::reinterpret_cast(&v);
189-
let fill = (**repr).unboxed.fill;
190-
(**repr).unboxed.fill += sys::size_of::<T>();
191-
let p = addr_of(&((**repr).unboxed.data));
192-
let p = ptr::offset(p, fill) as *mut T;
193-
rusti::move_val_init(*p, move initval);
194-
}
195-
// This doesn't bother to make sure we have space.
196-
#[cfg(stage1)]
197-
#[cfg(stage2)]
198180
#[inline(always)] // really pretty please
199181
pub unsafe fn push_fast<T>(v: &mut @[const T], initval: T) {
200182
let repr: **VecRepr = ::cast::reinterpret_cast(&v);

trunk/src/libcore/vec.rs

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ extern mod rustrt {
1818

1919
#[abi = "rust-intrinsic"]
2020
extern mod rusti {
21-
#[cfg(stage0)]
22-
fn move_val_init<T>(&dst: T, -src: T);
23-
#[cfg(stage1)]
24-
#[cfg(stage2)]
2521
fn move_val_init<T>(dst: &mut T, -src: T);
2622
}
2723

@@ -103,23 +99,6 @@ pub pure fn len<T>(v: &[const T]) -> uint {
10399
* Creates an immutable vector of size `n_elts` and initializes the elements
104100
* to the value returned by the function `op`.
105101
*/
106-
#[cfg(stage0)]
107-
pub pure fn from_fn<T>(n_elts: uint, op: iter::InitOp<T>) -> ~[T] {
108-
unsafe {
109-
let mut v = with_capacity(n_elts);
110-
do as_mut_buf(v) |p, _len| {
111-
let mut i: uint = 0u;
112-
while i < n_elts {
113-
rusti::move_val_init(*ptr::mut_offset(p, i), op(i));
114-
i += 1u;
115-
}
116-
}
117-
raw::set_len(&mut v, n_elts);
118-
return move v;
119-
}
120-
}
121-
#[cfg(stage1)]
122-
#[cfg(stage2)]
123102
pub pure fn from_fn<T>(n_elts: uint, op: iter::InitOp<T>) -> ~[T] {
124103
unsafe {
125104
let mut v = with_capacity(n_elts);
@@ -503,19 +482,6 @@ pub fn push<T>(v: &mut ~[T], initval: T) {
503482
}
504483
}
505484

506-
#[cfg(stage0)]
507-
// This doesn't bother to make sure we have space.
508-
#[inline(always)] // really pretty please
509-
unsafe fn push_fast<T>(v: &mut ~[T], initval: T) {
510-
let repr: **raw::VecRepr = ::cast::transmute(v);
511-
let fill = (**repr).unboxed.fill;
512-
(**repr).unboxed.fill += sys::size_of::<T>();
513-
let p = addr_of(&((**repr).unboxed.data));
514-
let p = ptr::offset(p, fill) as *mut T;
515-
rusti::move_val_init(*p, move initval);
516-
}
517-
#[cfg(stage1)]
518-
#[cfg(stage2)]
519485
// This doesn't bother to make sure we have space.
520486
#[inline(always)] // really pretty please
521487
unsafe fn push_fast<T>(v: &mut ~[T], initval: T) {
@@ -1793,18 +1759,6 @@ pub mod raw {
17931759
as_const_buf(v, |p, _len| *ptr::const_offset(p, i))
17941760
}
17951761

1796-
#[cfg(stage0)]
1797-
#[inline(always)]
1798-
pub unsafe fn init_elem<T>(v: &[mut T], i: uint, val: T) {
1799-
let mut box = Some(move val);
1800-
do as_mut_buf(v) |p, _len| {
1801-
let mut box2 = None;
1802-
box2 <-> box;
1803-
rusti::move_val_init(*ptr::mut_offset(p, i),
1804-
option::unwrap(move box2));
1805-
}
1806-
}
1807-
#[cfg(stage1)]
18081762
/**
18091763
* Unchecked vector index assignment. Does not drop the
18101764
* old value and hence is only suitable when the vector

0 commit comments

Comments
 (0)