Handle fallout in documentation
Tweak the tutorial's section on vectors and strings, to slightly clarify the difference between fixed-size vectors, vectors, and slices.
This commit is contained in:
parent
752048a271
commit
eab6bb2ece
8 changed files with 81 additions and 79 deletions
|
|
@ -266,8 +266,8 @@ Iterators offer generic conversion to containers with the `collect` adaptor:
|
|||
|
||||
~~~
|
||||
let xs = [0, 1, 1, 2, 3, 5, 8];
|
||||
let ys = xs.iter().rev().skip(1).map(|&x| x * 2).collect::<~[int]>();
|
||||
assert_eq!(ys, ~[10, 6, 4, 2, 2, 0]);
|
||||
let ys = xs.iter().rev().skip(1).map(|&x| x * 2).collect::<Vec<int>>();
|
||||
assert_eq!(ys, vec![10, 6, 4, 2, 2, 0]);
|
||||
~~~
|
||||
|
||||
The method requires a type hint for the container type, if the surrounding code
|
||||
|
|
@ -278,14 +278,14 @@ implementing the `FromIterator` trait. For example, the implementation for
|
|||
vectors is as follows:
|
||||
|
||||
~~~ {.ignore}
|
||||
impl<A> FromIterator<A> for ~[A] {
|
||||
pub fn from_iter<T: Iterator<A>>(iterator: &mut T) -> ~[A] {
|
||||
impl<T> FromIterator<T> for Vec<T> {
|
||||
fn from_iter<I:Iterator<A>>(mut iterator: I) -> Vec<T> {
|
||||
let (lower, _) = iterator.size_hint();
|
||||
let mut xs = with_capacity(lower);
|
||||
for x in iterator {
|
||||
xs.push(x);
|
||||
let mut vector = Vec::with_capacity(lower);
|
||||
for element in iterator {
|
||||
vector.push(element);
|
||||
}
|
||||
xs
|
||||
vector
|
||||
}
|
||||
}
|
||||
~~~
|
||||
|
|
|
|||
|
|
@ -3598,18 +3598,18 @@ and the cast expression in `main`.
|
|||
Within the body of an item that has type parameter declarations, the names of its type parameters are types:
|
||||
|
||||
~~~~
|
||||
fn map<A: Clone, B: Clone>(f: |A| -> B, xs: &[A]) -> ~[B] {
|
||||
fn map<A: Clone, B: Clone>(f: |A| -> B, xs: &[A]) -> Vec<B> {
|
||||
if xs.len() == 0 {
|
||||
return ~[];
|
||||
return vec![];
|
||||
}
|
||||
let first: B = f(xs[0].clone());
|
||||
let rest: ~[B] = map(f, xs.slice(1, xs.len()));
|
||||
return ~[first] + rest;
|
||||
let rest: Vec<B> = map(f, xs.slice(1, xs.len()));
|
||||
return [first] + rest;
|
||||
}
|
||||
~~~~
|
||||
|
||||
Here, `first` has type `B`, referring to `map`'s `B` type parameter;
|
||||
and `rest` has type `~[B]`, a vector type with element type `B`.
|
||||
and `rest` has type `Vec<B>`, a vector type with element type `B`.
|
||||
|
||||
### Self types
|
||||
|
||||
|
|
|
|||
|
|
@ -1588,8 +1588,8 @@ let mut numbers = vec![1, 2, 3];
|
|||
numbers.push(4);
|
||||
numbers.push(5);
|
||||
|
||||
// The type of a unique vector is written as `~[int]`
|
||||
let more_numbers: ~[int] = numbers.move_iter().collect();
|
||||
// The type of a unique vector is written as `Vec<int>`
|
||||
let more_numbers: Vec<int> = numbers.move_iter().map(|i| i+1).collect();
|
||||
|
||||
// The original `numbers` value can no longer be used, due to move semantics.
|
||||
|
||||
|
|
@ -1633,7 +1633,7 @@ view[0] = 5;
|
|||
let ys: &mut [int] = &mut [1, 2, 3];
|
||||
~~~
|
||||
|
||||
Square brackets denote indexing into a vector:
|
||||
Square brackets denote indexing into a slice or fixed-size vector:
|
||||
|
||||
~~~~
|
||||
# enum Crayon { Almond, AntiqueBrass, Apricot,
|
||||
|
|
@ -1647,7 +1647,7 @@ match crayons[0] {
|
|||
}
|
||||
~~~~
|
||||
|
||||
A vector can be destructured using pattern matching:
|
||||
A slice or fixed-size vector can be destructured using pattern matching:
|
||||
|
||||
~~~~
|
||||
let numbers: &[int] = &[1, 2, 3];
|
||||
|
|
@ -1660,9 +1660,10 @@ let score = match numbers {
|
|||
~~~~
|
||||
|
||||
Both vectors and strings support a number of useful [methods](#methods),
|
||||
defined in [`std::vec`] and [`std::str`].
|
||||
defined in [`std::vec`], [`std::slice`], and [`std::str`].
|
||||
|
||||
[`std::vec`]: std/vec/index.html
|
||||
[`std::slice`]: std/slice/index.html
|
||||
[`std::str`]: std/str/index.html
|
||||
|
||||
# Ownership escape hatches
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue