diff options
Diffstat (limited to 'src/doc/reference.md')
| -rw-r--r-- | src/doc/reference.md | 66 |
1 files changed, 40 insertions, 26 deletions
diff --git a/src/doc/reference.md b/src/doc/reference.md index febd5f883d1..804b6b9f63c 100644 --- a/src/doc/reference.md +++ b/src/doc/reference.md @@ -1588,10 +1588,11 @@ pointer values (pointing to a type for which an implementation of the given trait is in scope) to pointers to the trait name, used as a type. ``` +# use std::boxed::Box; # trait Shape { } # impl Shape for int { } # let mycircle = 0i; -let myshape: Box<Shape> = box mycircle as Box<Shape>; +let myshape: Box<Shape> = Box::new(mycircle) as Box<Shape>; ``` The resulting value is a box containing the value that was cast, along with @@ -1646,12 +1647,13 @@ fn radius_times_area<T: Circle>(c: T) -> f64 { Likewise, supertrait methods may also be called on trait objects. ```{.ignore} +# use std::boxed::Box; # trait Shape { fn area(&self) -> f64; } # trait Circle : Shape { fn radius(&self) -> f64; } # impl Shape for int { fn area(&self) -> f64 { 0.0 } } # impl Circle for int { fn radius(&self) -> f64 { 0.0 } } # let mycircle = 0; -let mycircle = box mycircle as Box<Circle>; +let mycircle = Box::new(mycircle) as Box<Circle>; let nonsense = mycircle.radius() * mycircle.area(); ``` @@ -3376,14 +3378,17 @@ stands for a *single* data field, whereas a wildcard `..` stands for *all* the fields of a particular variant. For example: ``` +#![feature(box_syntax)] enum List<X> { Nil, Cons(X, Box<List<X>>) } -let x: List<int> = List::Cons(10, box List::Cons(11, box List::Nil)); +fn main() { + let x: List<int> = List::Cons(10, box List::Cons(11, box List::Nil)); -match x { - List::Cons(_, box List::Nil) => panic!("singleton list"), - List::Cons(..) => return, - List::Nil => panic!("empty list") + match x { + List::Cons(_, box List::Nil) => panic!("singleton list"), + List::Cons(..) => return, + List::Nil => panic!("empty list") + } } ``` @@ -3436,25 +3441,28 @@ the inside of the match. An example of a `match` expression: ``` +#![feature(box_syntax)] # fn process_pair(a: int, b: int) { } # fn process_ten() { } enum List<X> { Nil, Cons(X, Box<List<X>>) } -let x: List<int> = List::Cons(10, box List::Cons(11, box List::Nil)); +fn main() { + let x: List<int> = List::Cons(10, box List::Cons(11, box List::Nil)); -match x { - List::Cons(a, box List::Cons(b, _)) => { - process_pair(a, b); - } - List::Cons(10, _) => { - process_ten(); - } - List::Nil => { - return; - } - _ => { - panic!(); + match x { + List::Cons(a, box List::Cons(b, _)) => { + process_pair(a, b); + } + List::Cons(10, _) => { + process_ten(); + } + List::Nil => { + return; + } + _ => { + panic!(); + } } } ``` @@ -3468,6 +3476,8 @@ Subpatterns can also be bound to variables by the use of the syntax `variable @ subpattern`. For example: ``` +#![feature(box_syntax)] + enum List { Nil, Cons(uint, Box<List>) } fn is_sorted(list: &List) -> bool { @@ -3781,12 +3791,13 @@ enclosing `enum` or `struct` type itself. Such recursion has restrictions: An example of a *recursive* type and its use: ``` +# use std::boxed::Box; enum List<T> { - Nil, - Cons(T, Box<List<T>>) + Nil, + Cons(T, Box<List<T>>) } -let a: List<int> = List::Cons(7, box List::Cons(13, box List::Nil)); +let a: List<int> = List::Cons(7, Box::new(List::Cons(13, Box::new(List::Nil)))); ``` ### Pointer types @@ -3893,6 +3904,7 @@ implementation of `R`, and the pointer value of `E`. An example of an object type: ``` +# use std::boxed::Box; trait Printable { fn stringify(&self) -> String; } @@ -3906,7 +3918,7 @@ fn print(a: Box<Printable>) { } fn main() { - print(box 10i as Box<Printable>); + print(Box::new(10i) as Box<Printable>); } ``` @@ -4100,7 +4112,8 @@ the type of a box is `std::owned::Box<T>`. An example of a box type and value: ``` -let x: Box<int> = box 10; +# use std::boxed::Box; +let x: Box<int> = Box::new(10); ``` Box values exist in 1:1 correspondence with their heap allocation, copying a @@ -4109,7 +4122,8 @@ copy of a box to move ownership of the value. After a value has been moved, the source location cannot be used unless it is reinitialized. ``` -let x: Box<int> = box 10; +# use std::boxed::Box; +let x: Box<int> = Box::new(10); let y = x; // attempting to use `x` will result in an error here ``` |
