@@ -455,12 +455,13 @@ fn rc_succ(x: Rc<int>) -> int { *x + 1 }
455
455
Note that the caller of your function will have to modify their calls slightly:
456
456
457
457
``` {rust}
458
+ # use std::boxed::Box;
458
459
use std::rc::Rc;
459
460
460
461
fn succ(x: &int) -> int { *x + 1 }
461
462
462
463
let ref_x = &5i;
463
- let box_x = box 5i ;
464
+ let box_x = Box::new(5i) ;
464
465
let rc_x = Rc::new(5i);
465
466
466
467
succ(ref_x);
@@ -477,24 +478,17 @@ those contents.
477
478
heap allocation in Rust. Creating a box looks like this:
478
479
479
480
``` {rust}
480
- let x = box(std::boxed::HEAP) 5i;
481
+ # use std::boxed::Box;
482
+ let x = Box::new(5i);
481
483
```
482
484
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:
494
487
495
488
``` {rust}
489
+ # use std::boxed::Box;
496
490
{
497
- let x = box 5i ;
491
+ let x = Box::new(5i) ;
498
492
499
493
// stuff happens
500
494
@@ -513,8 +507,9 @@ You don't need to fully grok the theory of affine types or regions to grok
513
507
boxes, though. As a rough approximation, you can treat this Rust code:
514
508
515
509
``` {rust}
510
+ # use std::boxed::Box;
516
511
{
517
- let x = box 5i ;
512
+ let x = Box::new(5i) ;
518
513
519
514
// stuff happens
520
515
}
@@ -553,12 +548,13 @@ for more detail on how lifetimes work.
553
548
Using boxes and references together is very common. For example:
554
549
555
550
``` {rust}
551
+ # use std::boxed::Box;
556
552
fn add_one(x: &int) -> int {
557
553
*x + 1
558
554
}
559
555
560
556
fn main() {
561
- let x = box 5i ;
557
+ let x = Box::new(5i) ;
562
558
563
559
println!("{}", add_one(&*x));
564
560
}
@@ -570,12 +566,13 @@ function, and since it's only reading the value, allows it.
570
566
We can borrow ` x ` multiple times, as long as it's not simultaneous:
571
567
572
568
``` {rust}
569
+ # use std::boxed::Box;
573
570
fn add_one(x: &int) -> int {
574
571
*x + 1
575
572
}
576
573
577
574
fn main() {
578
- let x = box 5i ;
575
+ let x = Box::new(5i) ;
579
576
580
577
println!("{}", add_one(&*x));
581
578
println!("{}", add_one(&*x));
@@ -586,12 +583,13 @@ fn main() {
586
583
Or as long as it's not a mutable borrow. This will error:
587
584
588
585
``` {rust,ignore}
586
+ # use std::boxed::Box;
589
587
fn add_one(x: &mut int) -> int {
590
588
*x + 1
591
589
}
592
590
593
591
fn main() {
594
- let x = box 5i ;
592
+ let x = Box::new(5i) ;
595
593
596
594
println!("{}", add_one(&*x)); // error: cannot borrow immutable dereference
597
595
// of `&`-pointer as mutable
@@ -612,22 +610,23 @@ Sometimes, you need a recursive data structure. The simplest is known as a
612
610
613
611
614
612
``` {rust}
615
- #[deriving(Show)]
613
+ # use std::boxed::Box;
614
+ #[derive(Show)]
616
615
enum List<T> {
617
616
Cons(T, Box<List<T>>),
618
617
Nil,
619
618
}
620
619
621
620
fn 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))) )));
623
622
println!("{:?}", list);
624
623
}
625
624
```
626
625
627
626
This prints:
628
627
629
628
``` text
630
- Cons(1, box Cons(2, box Cons(3, box Nil)))
629
+ Cons(1, Box( Cons(2, Box( Cons(3, Box( Nil))) )))
631
630
```
632
631
633
632
The 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
667
666
so as to avoid copying a large data structure. For example:
668
667
669
668
``` {rust}
669
+ # use std::boxed::Box;
670
670
struct BigStruct {
671
671
one: int,
672
672
two: int,
@@ -675,15 +675,15 @@ struct BigStruct {
675
675
}
676
676
677
677
fn foo(x: Box<BigStruct>) -> Box<BigStruct> {
678
- return box *x ;
678
+ return Box::new(*x) ;
679
679
}
680
680
681
681
fn main() {
682
- let x = box BigStruct {
682
+ let x = Box::new( BigStruct {
683
683
one: 1,
684
684
two: 2,
685
685
one_hundred: 100,
686
- };
686
+ }) ;
687
687
688
688
let y = foo(x);
689
689
}
@@ -695,6 +695,7 @@ than the hundred `int`s that make up the `BigStruct`.
695
695
This is an antipattern in Rust. Instead, write this:
696
696
697
697
``` {rust}
698
+ # use std::boxed::Box;
698
699
struct BigStruct {
699
700
one: int,
700
701
two: int,
@@ -707,13 +708,13 @@ fn foo(x: Box<BigStruct>) -> BigStruct {
707
708
}
708
709
709
710
fn main() {
710
- let x = box BigStruct {
711
+ let x = Box::new( BigStruct {
711
712
one: 1,
712
713
two: 2,
713
714
one_hundred: 100,
714
- };
715
+ }) ;
715
716
716
- let y = box foo(x);
717
+ let y = Box::new( foo(x) );
717
718
}
718
719
```
719
720
0 commit comments