Skip to content

Cleanup collections::slice documentation. #16279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 35 additions & 48 deletions src/libcollections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,69 +10,67 @@

/*!

Utilities for vector manipulation
Utilities for slice manipulation

The `vec` module contains useful code to help work with vector values.
Vectors are Rust's list type. Vectors contain zero or more values of
homogeneous types:
The `slice` module contains useful code to help work with slice values.
Slices are a view into a block of memory represented as a pointer and a length.

```rust
let int_vector = [1i, 2i, 3i];
let str_vector = ["one", "two", "three"];
// slicing a Vec
let vec = vec!(1i, 2, 3);
let int_slice = vec.as_slice();
// coercing an array to a slice
let str_slice: &[&str] = ["one", "two", "three"];
```

This is a big module, but for a high-level overview:
Slices are either mutable or shared. The shared slice type is `&[T]`,
while the mutable slice type is `&mut[T]`. For example, you can mutate the
block of memory that a mutable slice points to:

```rust
let x: &mut[int] = [1i, 2, 3];
x[1] = 7;
assert_eq!(x[0], 1);
assert_eq!(x[1], 7);
assert_eq!(x[2], 3);
```

Here are some of the things this module contains:

## Structs

Several structs that are useful for vectors, such as `Items`, which
represents iteration over a vector.
There are several structs that are useful for slices, such as `Items`, which
represents iteration over a slice.

## Traits
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The word "vector" in the section ## Structs may want to change to "slice"


A number of traits add methods that allow you to accomplish tasks with vectors.

Traits defined for the `&[T]` type (a vector slice), have methods that can be
called on either owned vectors, denoted `~[T]`, or on vector slices themselves.
These traits include `ImmutableVector`, and `MutableVector` for the `&mut [T]`
case.
A number of traits add methods that allow you to accomplish tasks with slices.
These traits include `ImmutableVector`, which is defined for `&[T]` types,
and `MutableVector`, defined for `&mut [T]` types.

An example is the method `.slice(a, b)` that returns an immutable "view" into
a vector or a vector slice from the index interval `[a, b)`:
a `Vec` or another slice from the index interval `[a, b)`:

```rust
let numbers = [0i, 1i, 2i];
let last_numbers = numbers.slice(1, 3);
// last_numbers is now &[1i, 2i]
```

Traits defined for the `~[T]` type, like `OwnedVector`, can only be called
on such vectors. These methods deal with adding elements or otherwise changing
the allocation of the vector.

An example is the method `.push(element)` that will add an element at the end
of the vector:

```rust
let mut numbers = vec![0i, 1i, 2i];
numbers.push(7);
// numbers is now vec![0i, 1i, 2i, 7i];
```

## Implementations of other traits

Vectors are a very useful type, and so there's several implementations of
traits from other modules. Some notable examples:
There are several implementations of common traits for slices. Some examples
include:

* `Clone`
* `Eq`, `Ord`, `Eq`, `Ord` -- vectors can be compared,
if the element type defines the corresponding trait.
* `Eq`, `Ord` - for immutable slices whose element type are `Eq` or `Ord`.
* `Hash` - for slices whose element type is `Hash`

## Iteration

The method `iter()` returns an iteration value for a vector or a vector slice.
The iterator yields references to the vector's elements, so if the element
type of the vector is `int`, the element type of the iterator is `&int`.
The method `iter()` returns an iteration value for a slice. The iterator
yields references to the slice's elements, so if the element
type of the slice is `int`, the element type of the iterator is `&int`.

```rust
let numbers = [0i, 1i, 2i];
Expand All @@ -82,18 +80,7 @@ for &x in numbers.iter() {
```

* `.mut_iter()` returns an iterator that allows modifying each value.
* `.move_iter()` converts an owned vector into an iterator that
moves out a value from the vector each iteration.
* Further iterators exist that split, chunk or permute the vector.

## Function definitions

There are a number of free functions that create or take vectors, for example:

* Creating a vector, like `from_elem` and `from_fn`
* Creating a vector with a given size: `with_capacity`
* Modifying a vector and returning it, like `append`
* Operations on paired elements, like `unzip`.
* Further iterators exist that split, chunk or permute the slice.

*/

Expand Down