Skip to content

Commit 14bc29c

Browse files
committed
---
yaml --- r: 22321 b: refs/heads/snap-stage3 c: b4f124e h: refs/heads/master i: 22319: e0aee4c v: v3
1 parent 7ebcfbe commit 14bc29c

File tree

2 files changed

+56
-45
lines changed

2 files changed

+56
-45
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: e430a699f2c60890d9b86069fd0c68a70ece7120
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 88ae10165b42bbd8fbad2d458b64c02e2b66e77b
4+
refs/heads/snap-stage3: b4f124e5f810b178d7413f0a9b4db97c83063464
55
refs/heads/try: ffbe0e0e00374358b789b0037bcb3a577cd218be
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/doc/tutorial.md

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,109 +1237,120 @@ pointers to vectors are also called 'slices'.
12371237
enum Crayon {
12381238
Almond, AntiqueBrass, Apricot,
12391239
Aquamarine, Asparagus, AtomicTangerine,
1240-
BananaMania, Beaver, Bittersweet
1240+
BananaMania, Beaver, Bittersweet,
1241+
Black, BlizzardBlue, Blue
12411242
}
12421243

12431244
// A fixed-size stack vector
12441245
let stack_crayons: [Crayon * 3] = [Almond, AntiqueBrass, Apricot];
12451246

12461247
// A borrowed pointer to stack allocated vector
1247-
let stack_crayons: &[Crayon] = &[Almond, AntiqueBrass, Apricot];
1248+
let stack_crayons: &[Crayon] = &[Aquamarine, Asparagus, AtomicTangerine];
12481249

12491250
// A local heap (managed) vector of crayons
1250-
let local_crayons: @[Crayon] = @[Aquamarine, Asparagus, AtomicTangerine];
1251+
let local_crayons: @[Crayon] = @[BananaMania, Beaver, Bittersweet];
12511252

12521253
// An exchange heap (owned) vector of crayons
1253-
let exchange_crayons: ~[Crayon] = ~[BananaMania, Beaver, Bittersweet];
1254+
let exchange_crayons: ~[Crayon] = ~[Black, BlizzardBlue, Blue];
12541255
~~~
12551256
1256-
Vector literals are enclosed in square brackets and dereferencing is
1257-
also done with square brackets (zero-based):
1257+
The `+` operator means concatenation when applied to vector types.
12581258
12591259
~~~~
12601260
# enum Crayon { Almond, AntiqueBrass, Apricot,
12611261
# Aquamarine, Asparagus, AtomicTangerine,
12621262
# BananaMania, Beaver, Bittersweet };
1263-
# fn draw_scene(c: Crayon) { }
12641263
1265-
let crayons: [Crayon * 3] = [BananaMania, Beaver, Bittersweet];
1266-
match crayons[0] {
1267-
Bittersweet => draw_scene(crayons[0]),
1268-
_ => ()
1269-
}
1264+
let my_crayons = ~[Almond, AntiqueBrass, Apricot];
1265+
let your_crayons = ~[BananaMania, Beaver, Bittersweet];
1266+
1267+
// Add two vectors to create a new one
1268+
let our_crayons = my_crayons + your_crayons;
1269+
1270+
// += will append to a vector, provided it leves
1271+
// in a mutable slot
1272+
let mut my_crayons = move my_crayons;
1273+
my_crayons += your_crayons;
12701274
~~~~
12711275
1272-
By default, vectors are immutable—you can not replace their elements.
1273-
The type written as `[mut T]` is a vector with mutable
1274-
elements. Mutable vector literals are written `[mut]` (empty) or `[mut
1275-
1, 2, 3]` (with elements).
1276+
> ***Note:*** The above examples of vector addition use owned
1277+
> vectors. Some operations on slices and stack vectors are
1278+
> not well supported yet, owned vectors are often the most
1279+
> usable.
1280+
1281+
Indexing into vectors is done with square brackets:
12761282
12771283
~~~~
12781284
# enum Crayon { Almond, AntiqueBrass, Apricot,
12791285
# Aquamarine, Asparagus, AtomicTangerine,
12801286
# BananaMania, Beaver, Bittersweet };
1281-
1282-
let crayons: [mut Crayon * 3] = [mut BananaMania, Beaver, Bittersweet];
1283-
crayons[0] = AtomicTangerine;
1287+
# fn draw_scene(c: Crayon) { }
1288+
let crayons: [Crayon * 3] = [BananaMania, Beaver, Bittersweet];
1289+
match crayons[0] {
1290+
Bittersweet => draw_scene(crayons[0]),
1291+
_ => ()
1292+
}
12841293
~~~~
12851294
1286-
The `+` operator means concatenation when applied to vector types.
1295+
The elements of a vector _inherit the mutability of the vector_,
1296+
and as such individual elements may not be reassigned when the
1297+
vector lives in an immutable slot.
12871298
1288-
~~~~
1299+
~~~ {.xfail-test}
12891300
# enum Crayon { Almond, AntiqueBrass, Apricot,
12901301
# Aquamarine, Asparagus, AtomicTangerine,
12911302
# BananaMania, Beaver, Bittersweet };
1303+
let crayons: ~[Crayon] = ~[BananaMania, Beaver, Bittersweet];
12921304
1293-
let my_crayons = ~[Almond, AntiqueBrass, Apricot];
1294-
let your_crayons = ~[BananaMania, Beaver, Bittersweet];
1295-
1296-
let our_crayons = my_crayons + your_crayons;
1297-
~~~~
1305+
crayons[0] = Apricot; // ERROR: Can't assign to immutable vector
1306+
~~~
12981307

1299-
The `+=` operator also works as expected, provided the assignee
1300-
lives in a mutable slot.
1308+
Moving it into a mutable slot makes the elements assignable.
13011309

1302-
~~~~
1310+
~~~
13031311
# enum Crayon { Almond, AntiqueBrass, Apricot,
13041312
# Aquamarine, Asparagus, AtomicTangerine,
13051313
# BananaMania, Beaver, Bittersweet };
1314+
let crayons: ~[Crayon] = ~[BananaMania, Beaver, Bittersweet];
13061315
1307-
let mut my_crayons = ~[Almond, AntiqueBrass, Apricot];
1308-
let your_crayons = ~[BananaMania, Beaver, Bittersweet];
1316+
// Put the vector into a mutable slot
1317+
let mut mutable_crayons = move crayons;
13091318
1310-
my_crayons += your_crayons;
1311-
~~~~
1319+
// Now it's mutable to the bone
1320+
mutable_crayons[0] = Apricot;
1321+
~~~
13121322

1313-
> ***Note:*** The above examples of vector addition use owned
1314-
> vectors. Some operations on slices and stack vectors are
1315-
> not well supported yet, owned vectors are often the most
1316-
> usable.
1323+
This is a simple example of Rust's _dual-mode data structures_, also
1324+
referred to as _freezing and thawing_.
13171325

13181326
Strings are implemented with vectors of `[u8]`, though they have a distinct
13191327
type. They support most of the same allocation options as
13201328
vectors, though the string literal without a storage sigil, e.g.
13211329
`"foo"` is treated differently than a comparable vector (`[foo]`).
13221330
Whereas plain vectors are stack-allocated fixed-length vectors,
1323-
plain strings are region pointers to read-only memory.
1331+
plain strings are region pointers to read-only memory. Strings
1332+
are always immutable.
13241333

13251334
~~~
13261335
// A plain string is a slice to read-only (static) memory
13271336
let stack_crayons: &str = "Almond, AntiqueBrass, Apricot";
13281337
13291338
// The same thing, but with the `&`
1330-
let stack_crayons: &str = &"Almond, AntiqueBrass, Apricot";
1339+
let stack_crayons: &str = &"Aquamarine, Asparagus, AtomicTangerine";
13311340
13321341
// A local heap (managed) string
1333-
let local_crayons: @str = @"Aquamarine, Asparagus, AtomicTangerine";
1342+
let local_crayons: @str = @"BananMania, Beaver, Bittersweet";
13341343
13351344
// An exchange heap (owned) string
1336-
let exchange_crayons: ~str = ~"BananaMania, Beaver, Bittersweet";
1345+
let exchange_crayons: ~str = ~"Black, BlizzardBlue, Blue";
13371346
~~~
13381347

13391348
Both vectors and strings support a number of useful
1340-
[methods](#implementation). While we haven't covered methods yet,
1341-
most vector functionality is provided by methods, so let's have a
1342-
brief look at a few common ones.
1349+
[methods](#functions-and-methods), defined in [`core::vec`]
1350+
and [`core::str`]. Here are some examples.
1351+
1352+
[`core::vec`]: core/vec.html
1353+
[`core::str`]: core/str.html
13431354

13441355
~~~
13451356
# use io::println;

0 commit comments

Comments
 (0)