@@ -1237,109 +1237,120 @@ pointers to vectors are also called 'slices'.
1237
1237
enum Crayon {
1238
1238
Almond, AntiqueBrass, Apricot,
1239
1239
Aquamarine, Asparagus, AtomicTangerine,
1240
- BananaMania, Beaver, Bittersweet
1240
+ BananaMania, Beaver, Bittersweet,
1241
+ Black, BlizzardBlue, Blue
1241
1242
}
1242
1243
1243
1244
// A fixed-size stack vector
1244
1245
let stack_crayons: [ Crayon * 3] = [ Almond, AntiqueBrass, Apricot] ;
1245
1246
1246
1247
// A borrowed pointer to stack allocated vector
1247
- let stack_crayons: &[ Crayon] = &[ Almond, AntiqueBrass, Apricot ] ;
1248
+ let stack_crayons: &[ Crayon] = &[ Aquamarine, Asparagus, AtomicTangerine ] ;
1248
1249
1249
1250
// A local heap (managed) vector of crayons
1250
- let local_crayons: @[ Crayon] = @[ Aquamarine, Asparagus, AtomicTangerine ] ;
1251
+ let local_crayons: @[ Crayon] = @[ BananaMania, Beaver, Bittersweet ] ;
1251
1252
1252
1253
// An exchange heap (owned) vector of crayons
1253
- let exchange_crayons: ~ [ Crayon] = ~ [ BananaMania, Beaver, Bittersweet ] ;
1254
+ let exchange_crayons: ~ [ Crayon] = ~ [ Black, BlizzardBlue, Blue ] ;
1254
1255
~~~
1255
1256
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.
1258
1258
1259
1259
~~~~
1260
1260
# enum Crayon { Almond, AntiqueBrass, Apricot,
1261
1261
# Aquamarine, Asparagus, AtomicTangerine,
1262
1262
# BananaMania, Beaver, Bittersweet };
1263
- # fn draw_scene(c: Crayon) { }
1264
1263
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;
1270
1274
~~~~
1271
1275
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:
1276
1282
1277
1283
~~~~
1278
1284
# enum Crayon { Almond, AntiqueBrass, Apricot,
1279
1285
# Aquamarine, Asparagus, AtomicTangerine,
1280
1286
# 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
+ }
1284
1293
~~~~
1285
1294
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.
1287
1298
1288
- ~~~~
1299
+ ~~~ {.xfail-test}
1289
1300
# enum Crayon { Almond, AntiqueBrass, Apricot,
1290
1301
# Aquamarine, Asparagus, AtomicTangerine,
1291
1302
# BananaMania, Beaver, Bittersweet };
1303
+ let crayons: ~[Crayon] = ~[BananaMania, Beaver, Bittersweet];
1292
1304
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
+ ~~~
1298
1307
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.
1301
1309
1302
- ~~~~
1310
+ ~~~
1303
1311
# enum Crayon { Almond, AntiqueBrass, Apricot,
1304
1312
# Aquamarine, Asparagus, AtomicTangerine,
1305
1313
# BananaMania, Beaver, Bittersweet };
1314
+ let crayons: ~[Crayon] = ~[BananaMania, Beaver, Bittersweet];
1306
1315
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 ;
1309
1318
1310
- my_crayons += your_crayons;
1311
- ~~~~
1319
+ // Now it's mutable to the bone
1320
+ mutable_crayons[0] = Apricot;
1321
+ ~~~
1312
1322
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_ .
1317
1325
1318
1326
Strings are implemented with vectors of ` [u8] ` , though they have a distinct
1319
1327
type. They support most of the same allocation options as
1320
1328
vectors, though the string literal without a storage sigil, e.g.
1321
1329
` "foo" ` is treated differently than a comparable vector (` [foo] ` ).
1322
1330
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.
1324
1333
1325
1334
~~~
1326
1335
// A plain string is a slice to read-only (static) memory
1327
1336
let stack_crayons: &str = "Almond, AntiqueBrass, Apricot";
1328
1337
1329
1338
// The same thing, but with the `&`
1330
- let stack_crayons: &str = &"Almond, AntiqueBrass, Apricot ";
1339
+ let stack_crayons: &str = &"Aquamarine, Asparagus, AtomicTangerine ";
1331
1340
1332
1341
// A local heap (managed) string
1333
- let local_crayons: @str = @"Aquamarine, Asparagus, AtomicTangerine ";
1342
+ let local_crayons: @str = @"BananMania, Beaver, Bittersweet ";
1334
1343
1335
1344
// An exchange heap (owned) string
1336
- let exchange_crayons: ~ str = ~ "BananaMania, Beaver, Bittersweet ";
1345
+ let exchange_crayons: ~str = ~"Black, BlizzardBlue, Blue ";
1337
1346
~~~
1338
1347
1339
1348
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
1343
1354
1344
1355
~~~
1345
1356
# use io::println;
0 commit comments