提交 faa7ba75 编写于 作者: B bors

auto merge of #14553 : reem/rust/nuke-owned-vectors, r=alexcrichton

I removed all remaining deprecated owned vectors from the docs. All example tests pass.
...@@ -53,9 +53,8 @@ To return a Borrowed String Slice (&str) use the str helper function ...@@ -53,9 +53,8 @@ To return a Borrowed String Slice (&str) use the str helper function
~~~ ~~~
use std::str; use std::str;
let bytes = ~[104u8,105u8]; let bytes = &[104u8,105u8];
let x: Option<&str> = str::from_utf8(bytes); let x: &str = str::from_utf8(bytes).unwrap();
let y: &str = x.unwrap();
~~~ ~~~
To return an Owned String use the str helper function To return an Owned String use the str helper function
...@@ -136,7 +135,7 @@ let index: Option<uint> = str.find_str("rand"); ...@@ -136,7 +135,7 @@ let index: Option<uint> = str.find_str("rand");
The [`Container`](../std/container/trait.Container.html) trait provides the `len` method. The [`Container`](../std/container/trait.Container.html) trait provides the `len` method.
~~~ ~~~
let u: ~[u32] = ~[0, 1, 2]; let u: Vec<u32> = vec![0, 1, 2];
let v: &[u32] = &[0, 1, 2, 3]; let v: &[u32] = &[0, 1, 2, 3];
let w: [u32, .. 5] = [0, 1, 2, 3, 4]; let w: [u32, .. 5] = [0, 1, 2, 3, 4];
...@@ -148,7 +147,7 @@ println!("u: {}, v: {}, w: {}", u.len(), v.len(), w.len()); // 3, 4, 5 ...@@ -148,7 +147,7 @@ println!("u: {}, v: {}, w: {}", u.len(), v.len(), w.len()); // 3, 4, 5
Use the [`iter`](../std/vec/trait.ImmutableVector.html#tymethod.iter) method. Use the [`iter`](../std/vec/trait.ImmutableVector.html#tymethod.iter) method.
~~~ ~~~
let values: ~[int] = ~[1, 2, 3, 4, 5]; let values: Vec<int> = vec![1, 2, 3, 4, 5];
for value in values.iter() { // value: &int for value in values.iter() { // value: &int
println!("{}", *value); println!("{}", *value);
} }
......
...@@ -85,7 +85,7 @@ To take as an argument a fragment of Rust code, write `$` followed by a name ...@@ -85,7 +85,7 @@ To take as an argument a fragment of Rust code, write `$` followed by a name
`foo`.) `foo`.)
* `expr` (an expression. Examples: `2 + 2`; `if true then { 1 } else { 2 }`; * `expr` (an expression. Examples: `2 + 2`; `if true then { 1 } else { 2 }`;
`f(42)`.) `f(42)`.)
* `ty` (a type. Examples: `int`, `~[(char, String)]`, `&T`.) * `ty` (a type. Examples: `int`, `Vec<(char, String)>`, `&T`.)
* `pat` (a pattern, usually appearing in a `match` or on the left-hand side of * `pat` (a pattern, usually appearing in a `match` or on the left-hand side of
a declaration. Examples: `Some(t)`; `(17, 'a')`; `_`.) a declaration. Examples: `Some(t)`; `(17, 'a')`; `_`.)
* `block` (a sequence of actions. Example: `{ log(error, "hi"); return 12; }`) * `block` (a sequence of actions. Example: `{ log(error, "hi"); return 12; }`)
......
...@@ -198,14 +198,14 @@ Typically, tasks do not share memory but instead communicate amongst each other ...@@ -198,14 +198,14 @@ Typically, tasks do not share memory but instead communicate amongst each other
``` ```
fn main() { fn main() {
let numbers = ~[1,2,3]; let numbers = vec![1,2,3];
let (tx, rx) = channel(); let (tx, rx) = channel();
tx.send(numbers); tx.send(numbers);
spawn(proc() { spawn(proc() {
let numbers = rx.recv(); let numbers = rx.recv();
println!("{}", numbers[0]); println!("{}", *numbers.get(0));
}) })
} }
``` ```
...@@ -237,18 +237,18 @@ try to modify the previous example to continue using the variable `numbers`: ...@@ -237,18 +237,18 @@ try to modify the previous example to continue using the variable `numbers`:
```ignore ```ignore
fn main() { fn main() {
let numbers = ~[1,2,3]; let numbers = vec![1,2,3];
let (tx, rx) = channel(); let (tx, rx) = channel();
tx.send(numbers); tx.send(numbers);
spawn(proc() { spawn(proc() {
let numbers = rx.recv(); let numbers = rx.recv();
println!("{}", numbers[0]); println!("{}", numbers.get(0));
}); });
// Try to print a number from the original task // Try to print a number from the original task
println!("{}", numbers[0]); println!("{}", *numbers.get(0));
} }
``` ```
...@@ -256,7 +256,7 @@ This will result an error indicating that the value is no longer in scope: ...@@ -256,7 +256,7 @@ This will result an error indicating that the value is no longer in scope:
```notrust ```notrust
concurrency.rs:12:20: 12:27 error: use of moved value: 'numbers' concurrency.rs:12:20: 12:27 error: use of moved value: 'numbers'
concurrency.rs:12 println!("{}", numbers[0]); concurrency.rs:12 println!("{}", numbers.get(0));
^~~~~~~ ^~~~~~~
``` ```
...@@ -267,7 +267,7 @@ Let's see an example that uses the `clone` method to create copies of the data: ...@@ -267,7 +267,7 @@ Let's see an example that uses the `clone` method to create copies of the data:
``` ```
fn main() { fn main() {
let numbers = ~[1,2,3]; let numbers = vec![1,2,3];
for num in range(0, 3) { for num in range(0, 3) {
let (tx, rx) = channel(); let (tx, rx) = channel();
...@@ -276,7 +276,7 @@ fn main() { ...@@ -276,7 +276,7 @@ fn main() {
spawn(proc() { spawn(proc() {
let numbers = rx.recv(); let numbers = rx.recv();
println!("{:d}", numbers[num as uint]); println!("{:d}", *numbers.get(num as uint));
}) })
} }
} }
...@@ -301,7 +301,7 @@ extern crate sync; ...@@ -301,7 +301,7 @@ extern crate sync;
use sync::Arc; use sync::Arc;
fn main() { fn main() {
let numbers = ~[1,2,3]; let numbers = vec![1,2,3];
let numbers = Arc::new(numbers); let numbers = Arc::new(numbers);
for num in range(0, 3) { for num in range(0, 3) {
...@@ -310,7 +310,7 @@ fn main() { ...@@ -310,7 +310,7 @@ fn main() {
spawn(proc() { spawn(proc() {
let numbers = rx.recv(); let numbers = rx.recv();
println!("{:d}", numbers[num as uint]); println!("{:d}", *numbers.get(num as uint));
}) })
} }
} }
...@@ -348,7 +348,7 @@ extern crate sync; ...@@ -348,7 +348,7 @@ extern crate sync;
use sync::{Arc, Mutex}; use sync::{Arc, Mutex};
fn main() { fn main() {
let numbers = ~[1,2,3]; let numbers = vec![1,2,3];
let numbers_lock = Arc::new(Mutex::new(numbers)); let numbers_lock = Arc::new(Mutex::new(numbers));
for num in range(0, 3) { for num in range(0, 3) {
...@@ -360,9 +360,13 @@ fn main() { ...@@ -360,9 +360,13 @@ fn main() {
// Take the lock, along with exclusive access to the underlying array // Take the lock, along with exclusive access to the underlying array
let mut numbers = numbers_lock.lock(); let mut numbers = numbers_lock.lock();
numbers[num as uint] += 1;
println!("{}", numbers[num as uint]); // This is ugly for now, but will be replaced by
// `numbers[num as uint] += 1` in the near future.
// See: https://github.com/mozilla/rust/issues/6515
*numbers.get_mut(num as uint) = *numbers.get_mut(num as uint) + 1;
println!("{}", *numbers.get(num as uint));
// When `numbers` goes out of scope the lock is dropped // When `numbers` goes out of scope the lock is dropped
}) })
......
...@@ -886,8 +886,8 @@ fn main() { ...@@ -886,8 +886,8 @@ fn main() {
// Equivalent to 'std::iter::range_step(0, 10, 2);' // Equivalent to 'std::iter::range_step(0, 10, 2);'
range_step(0, 10, 2); range_step(0, 10, 2);
// Equivalent to 'foo(~[std::option::Some(1.0), std::option::None]);' // Equivalent to 'foo(vec![std::option::Some(1.0), std::option::None]);'
foo(~[Some(1.0), None]); foo(vec![Some(1.0), None]);
} }
~~~~ ~~~~
...@@ -995,8 +995,8 @@ the function name. ...@@ -995,8 +995,8 @@ the function name.
fn iter<T>(seq: &[T], f: |T|) { fn iter<T>(seq: &[T], f: |T|) {
for elt in seq.iter() { f(elt); } for elt in seq.iter() { f(elt); }
} }
fn map<T, U>(seq: &[T], f: |T| -> U) -> ~[U] { fn map<T, U>(seq: &[T], f: |T| -> U) -> Vec<U> {
let mut acc = ~[]; let mut acc = vec![];
for elt in seq.iter() { acc.push(f(elt)); } for elt in seq.iter() { acc.push(f(elt)); }
acc acc
} }
...@@ -1159,10 +1159,10 @@ except that they have the `extern` modifier. ...@@ -1159,10 +1159,10 @@ except that they have the `extern` modifier.
~~~~ ~~~~
// Declares an extern fn, the ABI defaults to "C" // Declares an extern fn, the ABI defaults to "C"
extern fn new_vec() -> ~[int] { ~[] } extern fn new_int() -> int { 0 }
// Declares an extern fn with "stdcall" ABI // Declares an extern fn with "stdcall" ABI
extern "stdcall" fn new_vec_stdcall() -> ~[int] { ~[] } extern "stdcall" fn new_int_stdcall() -> int { 0 }
~~~~ ~~~~
Unlike normal functions, extern fns have an `extern "ABI" fn()`. Unlike normal functions, extern fns have an `extern "ABI" fn()`.
...@@ -1170,8 +1170,8 @@ This is the same type as the functions declared in an extern ...@@ -1170,8 +1170,8 @@ This is the same type as the functions declared in an extern
block. block.
~~~~ ~~~~
# extern fn new_vec() -> ~[int] { ~[] } # extern fn new_int() -> int { 0 }
let fptr: extern "C" fn() -> ~[int] = new_vec; let fptr: extern "C" fn() -> int = new_int;
~~~~ ~~~~
Extern functions may be called directly from Rust code as Rust uses large, Extern functions may be called directly from Rust code as Rust uses large,
...@@ -1509,7 +1509,7 @@ Implementation parameters are written after the `impl` keyword. ...@@ -1509,7 +1509,7 @@ Implementation parameters are written after the `impl` keyword.
~~~~ ~~~~
# trait Seq<T> { } # trait Seq<T> { }
impl<T> Seq<T> for ~[T] { impl<T> Seq<T> for Vec<T> {
/* ... */ /* ... */
} }
impl Seq<bool> for u32 { impl Seq<bool> for u32 {
...@@ -3347,7 +3347,7 @@ Such a definite-sized vector type is a first-class type, since its size is known ...@@ -3347,7 +3347,7 @@ Such a definite-sized vector type is a first-class type, since its size is known
A vector without such a size is said to be of _indefinite_ size, A vector without such a size is said to be of _indefinite_ size,
and is therefore not a _first-class_ type. and is therefore not a _first-class_ type.
An indefinite-size vector can only be instantiated through a pointer type, An indefinite-size vector can only be instantiated through a pointer type,
such as `&[T]` or `~[T]`. such as `&[T]` or `Vec<T>`.
The kind of a vector type depends on the kind of its element type, The kind of a vector type depends on the kind of its element type,
as with other simple structural types. as with other simple structural types.
......
...@@ -2062,7 +2062,7 @@ extern crate collections; ...@@ -2062,7 +2062,7 @@ extern crate collections;
type Set<T> = collections::HashMap<T, ()>; type Set<T> = collections::HashMap<T, ()>;
struct Stack<T> { struct Stack<T> {
elements: ~[T] elements: Vec<T>
} }
enum Option<T> { enum Option<T> {
...@@ -2320,7 +2320,7 @@ trait Seq<T> { ...@@ -2320,7 +2320,7 @@ trait Seq<T> {
fn length(&self) -> uint; fn length(&self) -> uint;
} }
impl<T> Seq<T> for ~[T] { impl<T> Seq<T> for Vec<T> {
fn length(&self) -> uint { self.len() } fn length(&self) -> uint { self.len() }
} }
~~~~ ~~~~
...@@ -2392,7 +2392,7 @@ generic types. ...@@ -2392,7 +2392,7 @@ generic types.
~~~~ ~~~~
# trait Printable { fn print(&self); } # trait Printable { fn print(&self); }
fn print_all<T: Printable>(printable_things: ~[T]) { fn print_all<T: Printable>(printable_things: Vec<T>) {
for thing in printable_things.iter() { for thing in printable_things.iter() {
thing.print(); thing.print();
} }
...@@ -2410,10 +2410,10 @@ as in this version of `print_all` that copies elements. ...@@ -2410,10 +2410,10 @@ as in this version of `print_all` that copies elements.
~~~ ~~~
# trait Printable { fn print(&self); } # trait Printable { fn print(&self); }
fn print_all<T: Printable + Clone>(printable_things: ~[T]) { fn print_all<T: Printable + Clone>(printable_things: Vec<T>) {
let mut i = 0; let mut i = 0;
while i < printable_things.len() { while i < printable_things.len() {
let copy_of_thing = printable_things[i].clone(); let copy_of_thing = printable_things.get(i).clone();
copy_of_thing.print(); copy_of_thing.print();
i += 1; i += 1;
} }
...@@ -2438,11 +2438,11 @@ However, consider this function: ...@@ -2438,11 +2438,11 @@ However, consider this function:
# fn new_circle() -> int { 1 } # fn new_circle() -> int { 1 }
trait Drawable { fn draw(&self); } trait Drawable { fn draw(&self); }
fn draw_all<T: Drawable>(shapes: ~[T]) { fn draw_all<T: Drawable>(shapes: Vec<T>) {
for shape in shapes.iter() { shape.draw(); } for shape in shapes.iter() { shape.draw(); }
} }
# let c: Circle = new_circle(); # let c: Circle = new_circle();
# draw_all(~[c]); # draw_all(vec![c]);
~~~~ ~~~~
You can call that on a vector of circles, or a vector of rectangles You can call that on a vector of circles, or a vector of rectangles
...@@ -2742,9 +2742,9 @@ mod farm { ...@@ -2742,9 +2742,9 @@ mod farm {
# pub type Chicken = int; # pub type Chicken = int;
# struct Human(int); # struct Human(int);
# impl Human { pub fn rest(&self) { } } # impl Human { pub fn rest(&self) { } }
# pub fn make_me_a_farm() -> Farm { Farm { chickens: ~[], farmer: Human(0) } } # pub fn make_me_a_farm() -> Farm { Farm { chickens: vec![], farmer: Human(0) } }
pub struct Farm { pub struct Farm {
chickens: ~[Chicken], chickens: Vec<Chicken>,
pub farmer: Human pub farmer: Human
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册