From 0033a8b269aebe9d88e5e4158ef2f0cdd630e92f Mon Sep 17 00:00:00 2001 From: Jonathan Reem Date: Fri, 30 May 2014 18:49:48 -0700 Subject: [PATCH 1/5] Remove deprecated owned vector from complement cheatsheet. --- src/doc/complement-cheatsheet.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/doc/complement-cheatsheet.md b/src/doc/complement-cheatsheet.md index f14b70afc7a..1c3352a829c 100644 --- a/src/doc/complement-cheatsheet.md +++ b/src/doc/complement-cheatsheet.md @@ -53,9 +53,8 @@ To return a Borrowed String Slice (&str) use the str helper function ~~~ use std::str; -let bytes = ~[104u8,105u8]; -let x: Option<&str> = str::from_utf8(bytes); -let y: &str = x.unwrap(); +let bytes = &[104u8,105u8]; +let x: &str = str::from_utf8(bytes).unwrap(); ~~~ To return an Owned String use the str helper function @@ -136,7 +135,7 @@ let index: Option = str.find_str("rand"); The [`Container`](../std/container/trait.Container.html) trait provides the `len` method. ~~~ -let u: ~[u32] = ~[0, 1, 2]; +let u: Vec = vec![0, 1, 2]; let v: &[u32] = &[0, 1, 2, 3]; 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 Use the [`iter`](../std/vec/trait.ImmutableVector.html#tymethod.iter) method. ~~~ -let values: ~[int] = ~[1, 2, 3, 4, 5]; +let values: Vec = vec![1, 2, 3, 4, 5]; for value in values.iter() { // value: &int println!("{}", *value); } From c3825cbb9dfd5605c507055c40e769a5f6800bab Mon Sep 17 00:00:00 2001 From: Jonathan Reem Date: Fri, 30 May 2014 19:03:17 -0700 Subject: [PATCH 2/5] Remove deprecated owned vector from intro. --- src/doc/intro.md | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/doc/intro.md b/src/doc/intro.md index 71356eba6d9..0e9114d7b76 100644 --- a/src/doc/intro.md +++ b/src/doc/intro.md @@ -198,14 +198,14 @@ Typically, tasks do not share memory but instead communicate amongst each other ``` fn main() { - let numbers = ~[1,2,3]; + let numbers = vec![1,2,3]; let (tx, rx) = channel(); tx.send(numbers); spawn(proc() { 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`: ```ignore fn main() { - let numbers = ~[1,2,3]; + let numbers = vec![1,2,3]; let (tx, rx) = channel(); tx.send(numbers); spawn(proc() { let numbers = rx.recv(); - println!("{}", numbers[0]); + println!("{}", numbers.get(0)); }); // 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: ```notrust 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: ``` fn main() { - let numbers = ~[1,2,3]; + let numbers = vec![1,2,3]; for num in range(0, 3) { let (tx, rx) = channel(); @@ -276,7 +276,7 @@ fn main() { spawn(proc() { let numbers = rx.recv(); - println!("{:d}", numbers[num as uint]); + println!("{:d}", *numbers.get(num as uint)); }) } } @@ -301,7 +301,7 @@ extern crate sync; use sync::Arc; fn main() { - let numbers = ~[1,2,3]; + let numbers = vec![1,2,3]; let numbers = Arc::new(numbers); for num in range(0, 3) { @@ -310,7 +310,7 @@ fn main() { spawn(proc() { let numbers = rx.recv(); - println!("{:d}", numbers[num as uint]); + println!("{:d}", *numbers.get(num as uint)); }) } } @@ -348,7 +348,7 @@ extern crate sync; use sync::{Arc, Mutex}; fn main() { - let numbers = ~[1,2,3]; + let numbers = vec![1,2,3]; let numbers_lock = Arc::new(Mutex::new(numbers)); for num in range(0, 3) { @@ -360,9 +360,13 @@ fn main() { // Take the lock, along with exclusive access to the underlying array 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 }) From f740e8dde1a9ffa11865eb0d980423192142f049 Mon Sep 17 00:00:00 2001 From: Jonathan Reem Date: Fri, 30 May 2014 19:05:47 -0700 Subject: [PATCH 3/5] Remove deprecated owned vector from macro guide. --- src/doc/guide-macros.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/doc/guide-macros.md b/src/doc/guide-macros.md index 12a0ee6931c..b86a6aa1b61 100644 --- a/src/doc/guide-macros.md +++ b/src/doc/guide-macros.md @@ -85,7 +85,7 @@ To take as an argument a fragment of Rust code, write `$` followed by a name `foo`.) * `expr` (an expression. Examples: `2 + 2`; `if true then { 1 } else { 2 }`; `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 a declaration. Examples: `Some(t)`; `(17, 'a')`; `_`.) * `block` (a sequence of actions. Example: `{ log(error, "hi"); return 12; }`) From 66ee71a51732f3d36cd6efe14ca1e02031d26fb3 Mon Sep 17 00:00:00 2001 From: Jonathan Reem Date: Fri, 30 May 2014 19:15:10 -0700 Subject: [PATCH 4/5] Remove deprecated owned vector from rust.md --- src/doc/rust.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/doc/rust.md b/src/doc/rust.md index bf4fd3dcc93..571e68bfaa4 100644 --- a/src/doc/rust.md +++ b/src/doc/rust.md @@ -886,8 +886,8 @@ fn main() { // Equivalent to 'std::iter::range_step(0, 10, 2);' range_step(0, 10, 2); - // Equivalent to 'foo(~[std::option::Some(1.0), std::option::None]);' - foo(~[Some(1.0), None]); + // Equivalent to 'foo(vec![std::option::Some(1.0), std::option::None]);' + foo(vec![Some(1.0), None]); } ~~~~ @@ -995,8 +995,8 @@ the function name. fn iter(seq: &[T], f: |T|) { for elt in seq.iter() { f(elt); } } -fn map(seq: &[T], f: |T| -> U) -> ~[U] { - let mut acc = ~[]; +fn map(seq: &[T], f: |T| -> U) -> Vec { + let mut acc = vec![]; for elt in seq.iter() { acc.push(f(elt)); } acc } @@ -1159,10 +1159,10 @@ except that they have the `extern` modifier. ~~~~ // 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 -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()`. @@ -1170,8 +1170,8 @@ This is the same type as the functions declared in an extern block. ~~~~ -# extern fn new_vec() -> ~[int] { ~[] } -let fptr: extern "C" fn() -> ~[int] = new_vec; +# extern fn new_int() -> int { 0 } +let fptr: extern "C" fn() -> int = new_int; ~~~~ 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. ~~~~ # trait Seq { } -impl Seq for ~[T] { +impl Seq for Vec { /* ... */ } impl Seq for u32 { @@ -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, and is therefore not a _first-class_ type. An indefinite-size vector can only be instantiated through a pointer type, -such as `&[T]` or `~[T]`. +such as `&[T]` or `Vec`. The kind of a vector type depends on the kind of its element type, as with other simple structural types. From 1959925e514d9ecd5149435a3530dbdf0191f117 Mon Sep 17 00:00:00 2001 From: Jonathan Reem Date: Fri, 30 May 2014 19:19:52 -0700 Subject: [PATCH 5/5] Remove deprecated owned vector from tutorial. --- src/doc/tutorial.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/doc/tutorial.md b/src/doc/tutorial.md index a22256650b8..8a759cf7980 100644 --- a/src/doc/tutorial.md +++ b/src/doc/tutorial.md @@ -2062,7 +2062,7 @@ extern crate collections; type Set = collections::HashMap; struct Stack { - elements: ~[T] + elements: Vec } enum Option { @@ -2320,7 +2320,7 @@ trait Seq { fn length(&self) -> uint; } -impl Seq for ~[T] { +impl Seq for Vec { fn length(&self) -> uint { self.len() } } ~~~~ @@ -2392,7 +2392,7 @@ generic types. ~~~~ # trait Printable { fn print(&self); } -fn print_all(printable_things: ~[T]) { +fn print_all(printable_things: Vec) { for thing in printable_things.iter() { thing.print(); } @@ -2410,10 +2410,10 @@ as in this version of `print_all` that copies elements. ~~~ # trait Printable { fn print(&self); } -fn print_all(printable_things: ~[T]) { +fn print_all(printable_things: Vec) { let mut i = 0; 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(); i += 1; } @@ -2438,11 +2438,11 @@ However, consider this function: # fn new_circle() -> int { 1 } trait Drawable { fn draw(&self); } -fn draw_all(shapes: ~[T]) { +fn draw_all(shapes: Vec) { for shape in shapes.iter() { shape.draw(); } } # 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 @@ -2742,9 +2742,9 @@ mod farm { # pub type Chicken = int; # struct Human(int); # 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 { - chickens: ~[Chicken], + chickens: Vec, pub farmer: Human }