@@ -455,12 +455,13 @@ fn rc_succ(x: Rc<int>) -> int { *x + 1 }
455455Note that the caller of your function will have to modify their calls slightly:
456456
457457``` {rust}
458+ # use std::boxed::Box;
458459use std::rc::Rc;
459460
460461fn succ(x: &int) -> int { *x + 1 }
461462
462463let ref_x = &5i;
463- let box_x = box 5i ;
464+ let box_x = Box::new(5i) ;
464465let rc_x = Rc::new(5i);
465466
466467succ(ref_x);
@@ -477,24 +478,17 @@ those contents.
477478heap allocation in Rust. Creating a box looks like this:
478479
479480``` {rust}
480- let x = box(std::boxed::HEAP) 5i;
481+ # use std::boxed::Box;
482+ let x = Box::new(5i);
481483```
482484
483- ` box ` is a keyword that does 'placement new,' which we'll talk about in a bit.
484- ` box ` will be useful for creating a number of heap-allocated types, but is not
485- quite finished yet. In the meantime, ` box ` 's type defaults to
486- ` std::boxed::HEAP ` , and so you can leave it off:
487-
488- ``` {rust}
489- let x = box 5i;
490- ```
491-
492- As you might assume from the ` HEAP ` , boxes are heap allocated. They are
493- deallocated automatically by Rust when they go out of scope:
485+ Boxes are heap allocated and they are deallocated automatically by Rust when
486+ they go out of scope:
494487
495488``` {rust}
489+ # use std::boxed::Box;
496490{
497- let x = box 5i ;
491+ let x = Box::new(5i) ;
498492
499493 // stuff happens
500494
@@ -513,8 +507,9 @@ You don't need to fully grok the theory of affine types or regions to grok
513507boxes, though. As a rough approximation, you can treat this Rust code:
514508
515509``` {rust}
510+ # use std::boxed::Box;
516511{
517- let x = box 5i ;
512+ let x = Box::new(5i) ;
518513
519514 // stuff happens
520515}
@@ -553,12 +548,13 @@ for more detail on how lifetimes work.
553548Using boxes and references together is very common. For example:
554549
555550``` {rust}
551+ # use std::boxed::Box;
556552fn add_one(x: &int) -> int {
557553 *x + 1
558554}
559555
560556fn main() {
561- let x = box 5i ;
557+ let x = Box::new(5i) ;
562558
563559 println!("{}", add_one(&*x));
564560}
@@ -570,12 +566,13 @@ function, and since it's only reading the value, allows it.
570566We can borrow ` x ` multiple times, as long as it's not simultaneous:
571567
572568``` {rust}
569+ # use std::boxed::Box;
573570fn add_one(x: &int) -> int {
574571 *x + 1
575572}
576573
577574fn main() {
578- let x = box 5i ;
575+ let x = Box::new(5i) ;
579576
580577 println!("{}", add_one(&*x));
581578 println!("{}", add_one(&*x));
@@ -586,12 +583,13 @@ fn main() {
586583Or as long as it's not a mutable borrow. This will error:
587584
588585``` {rust,ignore}
586+ # use std::boxed::Box;
589587fn add_one(x: &mut int) -> int {
590588 *x + 1
591589}
592590
593591fn main() {
594- let x = box 5i ;
592+ let x = Box::new(5i) ;
595593
596594 println!("{}", add_one(&*x)); // error: cannot borrow immutable dereference
597595 // of `&`-pointer as mutable
@@ -612,22 +610,23 @@ Sometimes, you need a recursive data structure. The simplest is known as a
612610
613611
614612``` {rust}
615- #[deriving(Show)]
613+ # use std::boxed::Box;
614+ #[derive(Show)]
616615enum List<T> {
617616 Cons(T, Box<List<T>>),
618617 Nil,
619618}
620619
621620fn main() {
622- let list: List<int> = List::Cons(1, box List::Cons(2, box List::Cons(3, box List::Nil)));
621+ let list: List<int> = List::Cons(1, Box::new( List::Cons(2, Box::new( List::Cons(3, Box::new( List::Nil))) )));
623622 println!("{:?}", list);
624623}
625624```
626625
627626This prints:
628627
629628``` text
630- Cons(1, box Cons(2, box Cons(3, box Nil)))
629+ Cons(1, Box( Cons(2, Box( Cons(3, Box( Nil))) )))
631630```
632631
633632The reference to another ` List ` inside of the ` Cons ` enum variant must be a box,
@@ -667,6 +666,7 @@ In many languages with pointers, you'd return a pointer from a function
667666so as to avoid copying a large data structure. For example:
668667
669668``` {rust}
669+ # use std::boxed::Box;
670670struct BigStruct {
671671 one: int,
672672 two: int,
@@ -675,15 +675,15 @@ struct BigStruct {
675675}
676676
677677fn foo(x: Box<BigStruct>) -> Box<BigStruct> {
678- return box *x ;
678+ return Box::new(*x) ;
679679}
680680
681681fn main() {
682- let x = box BigStruct {
682+ let x = Box::new( BigStruct {
683683 one: 1,
684684 two: 2,
685685 one_hundred: 100,
686- };
686+ }) ;
687687
688688 let y = foo(x);
689689}
@@ -695,6 +695,7 @@ than the hundred `int`s that make up the `BigStruct`.
695695This is an antipattern in Rust. Instead, write this:
696696
697697``` {rust}
698+ # use std::boxed::Box;
698699struct BigStruct {
699700 one: int,
700701 two: int,
@@ -707,13 +708,13 @@ fn foo(x: Box<BigStruct>) -> BigStruct {
707708}
708709
709710fn main() {
710- let x = box BigStruct {
711+ let x = Box::new( BigStruct {
711712 one: 1,
712713 two: 2,
713714 one_hundred: 100,
714- };
715+ }) ;
715716
716- let y = box foo(x);
717+ let y = Box::new( foo(x) );
717718}
718719```
719720
0 commit comments