Skip to content

Commit 2cfab77

Browse files
committed
---
yaml --- r: 138166 b: refs/heads/try c: 7134fc8 h: refs/heads/master v: v3
1 parent b046a5c commit 2cfab77

File tree

16 files changed

+297
-165
lines changed

16 files changed

+297
-165
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: b6e0d3a5bf4c88650a22f605f822e02c6b163580
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
5-
refs/heads/try: dbc4a4b53b91e687429db622626a6eb221252b04
5+
refs/heads/try: 7134fc8de6c50b48af688c5c530d63c788215663
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/doc/guide.md

Lines changed: 65 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,87 +1496,102 @@ low-level details matter, they really matter. Just remember that `String`s
14961496
allocate memory and control their data, while `&str`s are a reference to
14971497
another string, and you'll be all set.
14981498

1499-
# Vectors
1499+
# Arrays, Vectors, and Slices
15001500

1501-
Like many programming languages, Rust has a list type for when you want a list
1502-
of things. But similar to strings, Rust has different types to represent this
1503-
idea: `Vec<T>` (a 'vector'), `[T, .. N]` (an 'array'), and `&[T]` (a 'slice').
1504-
Whew!
1501+
Like many programming languages, Rust has list types to represent a sequence of
1502+
things. The most basic is the **array**, a fixed-size list of elements of the
1503+
same type. By default, arrays are immutable.
15051504

1506-
Vectors are similar to `String`s: they have a dynamic length, and they
1507-
allocate enough memory to fit. You can create a vector with the `vec!` macro:
1505+
```{rust}
1506+
let a = [1i, 2i, 3i];
1507+
let mut m = [1i, 2i, 3i];
1508+
```
1509+
1510+
You can create an array with a given number of elements, all initialized to the
1511+
same value, with `[val, ..N]` syntax. The compiler ensures that arrays are
1512+
always initialized.
15081513

15091514
```{rust}
1510-
let nums = vec![1i, 2i, 3i];
1515+
let a = [0i, ..20]; // Shorthand for array of 20 elements all initialized to 0
15111516
```
15121517

1513-
Notice that unlike the `println!` macro we've used in the past, we use square
1514-
brackets (`[]`) with `vec!`. Rust allows you to use either in either situation,
1515-
this is just convention.
1518+
Arrays have type `[T,..N]`. We'll talk about this `T` notation later, when we
1519+
cover generics.
15161520

1517-
You can create an array with just square brackets:
1521+
You can get the number of elements in an array `a` with `a.len()`, and use
1522+
`a.iter()` to iterate over them with a for loop. This code will print each
1523+
number in order:
15181524

15191525
```{rust}
1520-
let nums = [1i, 2i, 3i];
1521-
let nums = [1i, ..20]; // Shorthand for an array of 20 elements all initialized to 1
1526+
let a = [1i, 2, 3]; // Only the first item needs a type suffix
1527+
1528+
println!("a has {} elements", a.len());
1529+
for e in a.iter() {
1530+
println!("{}", e);
1531+
}
15221532
```
15231533

1524-
So what's the difference? An array has a fixed size, so you can't add or
1525-
subtract elements:
1534+
You can access a particular element of an array with **subscript notation**:
15261535

1527-
```{rust,ignore}
1528-
let mut nums = vec![1i, 2i, 3i];
1529-
nums.push(4i); // works
1536+
```{rust}
1537+
let names = ["Graydon", "Brian", "Niko"];
15301538
1531-
let mut nums = [1i, 2i, 3i];
1532-
nums.push(4i); // error: type `[int, .. 3]` does not implement any method
1533-
// in scope named `push`
1539+
println!("The second name is: {}", names[1]);
15341540
```
15351541

1536-
The `push()` method lets you append a value to the end of the vector. But
1537-
since arrays have fixed sizes, adding an element doesn't make any sense.
1538-
You can see how it has the exact type in the error message: `[int, .. 3]`.
1539-
An array of `int`s, with length 3.
1542+
Subscripts start at zero, like in most programming languages, so the first name
1543+
is `names[0]` and the second name is `names[1]`. The above example prints
1544+
`The second name is: Brian`. If you try to use a subscript that is not in the
1545+
array, you will get an error: array access is bounds-checked at run-time. Such
1546+
errant access is the source of many bugs in other systems programming
1547+
languages.
15401548

1541-
Similar to `&str`, a slice is a reference to another array. We can get a
1542-
slice from a vector by using the `as_slice()` method:
1549+
A **vector** is a dynamic or "growable" array, implemented as the standard
1550+
library type [`Vec<T>`](std/vec/) (we'll talk about what the `<T>` means
1551+
later). Vectors are to arrays what `String` is to `&str`. You can create them
1552+
with the `vec!` macro:
15431553

15441554
```{rust}
1545-
let vec = vec![1i, 2i, 3i];
1546-
let slice = vec.as_slice();
1555+
let v = vec![1i, 2, 3];
15471556
```
15481557

1549-
All three types implement an `iter()` method, which returns an iterator. We'll
1550-
talk more about the details of iterators later, but for now, the `iter()` method
1551-
allows you to write a `for` loop that prints out the contents of a vector, array,
1552-
or slice:
1558+
(Notice that unlike the `println!` macro we've used in the past, we use square
1559+
brackets `[]` with `vec!`. Rust allows you to use either in either situation,
1560+
this is just convention.)
15531561

1554-
```{rust}
1555-
let vec = vec![1i, 2i, 3i];
1562+
You can get the length of, iterate over, and subscript vectors just like
1563+
arrays. In addition, (mutable) vectors can grow automatically:
15561564

1557-
for i in vec.iter() {
1558-
println!("{}", i);
1559-
}
1565+
```{rust}
1566+
let mut nums = vec![1i, 2, 3];
1567+
nums.push(4);
1568+
println!("The length of nums is now {}", nums.len()); // Prints 4
15601569
```
15611570

1562-
This code will print each number in order, on its own line.
1571+
Vectors have many more useful methods.
15631572

1564-
You can access a particular element of a vector, array, or slice by using
1565-
**subscript notation**:
1573+
A **slice** is a reference to (or "view" into) an array. They are useful for
1574+
allowing safe, efficient access to a portion of an array without copying. For
1575+
example, you might want to reference just one line of a file read into memory.
1576+
By nature, a slice is not created directly, but from an existing variable.
1577+
Slices have a length, can be mutable or not, and in many ways behave like
1578+
arrays:
15661579

15671580
```{rust}
1568-
let names = ["Graydon", "Brian", "Niko"];
1581+
let a = [0i, 1, 2, 3, 4];
1582+
let middle = a.slice(1, 4); // A slice of a: just the elements [1,2,3]
15691583
1570-
println!("The second name is: {}", names[1]);
1584+
for e in middle.iter() {
1585+
println!("{}", e); // Prints 1, 2, 3
1586+
}
15711587
```
15721588

1573-
These subscripts start at zero, like in most programming languages, so the
1574-
first name is `names[0]` and the second name is `names[1]`. The above example
1575-
prints `The second name is: Brian`.
1589+
You can also take a slice of a vector, `String`, or `&str`, because they are
1590+
backed by arrays. Slices have type `&[T]`, which we'll talk about when we cover
1591+
generics.
15761592

1577-
There's a whole lot more to vectors, but that's enough to get started. We have
1578-
now learned all of the most basic Rust concepts. We're ready to start building
1579-
our guessing game, but we need to know how to do one last thing first: get
1593+
We have now learned all of the most basic Rust concepts. We're ready to start
1594+
building our guessing game, we just need to know one last thing: how to get
15801595
input from the keyboard. You can't have a guessing game without the ability to
15811596
guess!
15821597

branches/try/src/librustc/metadata/encoder.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,10 @@ fn encode_reexported_static_base_methods(ecx: &EncodeContext,
407407
method_did.def_id());
408408
match impl_item {
409409
ty::MethodTraitItem(ref m) => {
410-
if m.explicit_self ==
411-
ty::StaticExplicitSelfCategory {
412-
encode_reexported_static_method(rbml_w,
413-
exp,
414-
m.def_id,
415-
m.ident);
416-
}
410+
encode_reexported_static_method(rbml_w,
411+
exp,
412+
m.def_id,
413+
m.ident);
417414
}
418415
ty::TypeTraitItem(_) => {}
419416
}
@@ -434,8 +431,7 @@ fn encode_reexported_static_trait_methods(ecx: &EncodeContext,
434431
Some(trait_items) => {
435432
for trait_item in trait_items.iter() {
436433
match *trait_item {
437-
ty::MethodTraitItem(ref m) if m.explicit_self ==
438-
ty::StaticExplicitSelfCategory => {
434+
ty::MethodTraitItem(ref m) => {
439435
encode_reexported_static_method(rbml_w,
440436
exp,
441437
m.def_id,
@@ -1408,18 +1404,16 @@ fn encode_info_for_item(ecx: &EncodeContext,
14081404
encode_family(rbml_w,
14091405
fn_style_static_method_family(
14101406
method_ty.fty.fn_style));
1411-
1412-
let pty = ty::lookup_item_type(tcx,
1413-
method_def_id);
1414-
encode_bounds_and_type(rbml_w, ecx, &pty);
14151407
}
1416-
14171408
_ => {
14181409
encode_family(rbml_w,
14191410
style_fn_family(
14201411
method_ty.fty.fn_style));
14211412
}
14221413
}
1414+
let pty = ty::lookup_item_type(tcx,
1415+
method_def_id);
1416+
encode_bounds_and_type(rbml_w, ecx, &pty);
14231417

14241418
is_nonstatic_method = method_ty.explicit_self !=
14251419
ty::StaticExplicitSelfCategory;

branches/try/src/librustc/middle/astencode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,8 @@ impl tr for def::Def {
453453
},
454454
p)
455455
}
456-
def::DefMethod(did0, did1) => {
457-
def::DefMethod(did0.tr(dcx), did1.map(|did1| did1.tr(dcx)))
456+
def::DefMethod(did0, did1, p) => {
457+
def::DefMethod(did0.tr(dcx), did1.map(|did1| did1.tr(dcx)), p)
458458
}
459459
def::DefSelfTy(nid) => { def::DefSelfTy(dcx.tr_id(nid)) }
460460
def::DefMod(did) => { def::DefMod(did.tr(dcx)) }

branches/try/src/librustc/middle/def.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub enum Def {
4646
DefTyParamBinder(ast::NodeId), /* struct, impl or trait with ty params */
4747
DefRegion(ast::NodeId),
4848
DefLabel(ast::NodeId),
49-
DefMethod(ast::DefId /* method */, Option<ast::DefId> /* trait */),
49+
DefMethod(ast::DefId /* method */, Option<ast::DefId> /* trait */, MethodProvenance),
5050
}
5151

5252
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
@@ -62,7 +62,7 @@ impl Def {
6262
DefForeignMod(id) | DefStatic(id, _) |
6363
DefVariant(_, id, _) | DefTy(id, _) | DefAssociatedTy(id) |
6464
DefTyParam(_, id, _) | DefUse(id) | DefStruct(id) | DefTrait(id) |
65-
DefMethod(id, _) | DefConst(id) => {
65+
DefMethod(id, _, _) | DefConst(id) => {
6666
id
6767
}
6868
DefLocal(id) |

branches/try/src/librustc/middle/privacy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
811811
def::DefTy(_, true) => ck("enum"),
812812
def::DefTrait(..) => ck("trait"),
813813
def::DefStruct(..) => ck("struct"),
814-
def::DefMethod(_, Some(..)) => ck("trait method"),
814+
def::DefMethod(_, Some(..), _) => ck("trait method"),
815815
def::DefMethod(..) => ck("method"),
816816
def::DefMod(..) => ck("module"),
817817
_ => {}

0 commit comments

Comments
 (0)