@@ -574,7 +574,7 @@ fn main() {
574
574
```
575
575
576
576
We can mutably borrow ` x ` multiple times, but only if x itself is mutable, and
577
- it may not be * simultaneously* borrowed:
577
+ it may not be * simultaneously* borrowed:
578
578
579
579
``` {rust,ignore}
580
580
fn increment(x: &mut i32) {
@@ -595,8 +595,7 @@ Notice the signature of `increment()` requests a mutable reference.
595
595
596
596
## Best practices
597
597
598
- Boxes are appropriate to use in two situations: Recursive data structures,
599
- and occasionally, when returning data.
598
+ Boxes are most appropriate to use when defining recursive data structures.
600
599
601
600
### Recursive data structures
602
601
@@ -630,14 +629,6 @@ we don't know the size, and therefore, we need to heap allocate our list.
630
629
Working with recursive or other unknown-sized data structures is the primary
631
630
use-case for boxes.
632
631
633
- ### Returning data
634
-
635
- This is important enough to have its own section entirely. The TL;DR is this:
636
- you don't want to return pointers, even when you might in a language like C or
637
- C++.
638
-
639
- See [ Returning Pointers] ( #returning-pointers ) below for more.
640
-
641
632
# Rc and Arc
642
633
643
634
This part is coming soon.
@@ -654,79 +645,6 @@ This part is coming soon.
654
645
655
646
This part is coming soon.
656
647
657
- # Returning Pointers
658
-
659
- In many languages with pointers, you'd return a pointer from a function
660
- so as to avoid copying a large data structure. For example:
661
-
662
- ``` {rust}
663
- struct BigStruct {
664
- one: i32,
665
- two: i32,
666
- // etc
667
- one_hundred: i32,
668
- }
669
-
670
- fn foo(x: Box<BigStruct>) -> Box<BigStruct> {
671
- Box::new(*x)
672
- }
673
-
674
- fn main() {
675
- let x = Box::new(BigStruct {
676
- one: 1,
677
- two: 2,
678
- one_hundred: 100,
679
- });
680
-
681
- let y = foo(x);
682
- }
683
- ```
684
-
685
- The idea is that by passing around a box, you're only copying a pointer, rather
686
- than the hundred ` int ` s that make up the ` BigStruct ` .
687
-
688
- This is an antipattern in Rust. Instead, write this:
689
-
690
- ``` rust
691
- #![feature(box_syntax)]
692
-
693
- struct BigStruct {
694
- one : i32 ,
695
- two : i32 ,
696
- // etc
697
- one_hundred : i32 ,
698
- }
699
-
700
- fn foo (x : Box <BigStruct >) -> BigStruct {
701
- * x
702
- }
703
-
704
- fn main () {
705
- let x = Box :: new (BigStruct {
706
- one : 1 ,
707
- two : 2 ,
708
- one_hundred : 100 ,
709
- });
710
-
711
- let y : Box <BigStruct > = box foo (x );
712
- }
713
- ```
714
-
715
- Note that this uses the ` box_syntax ` feature gate, so this syntax may change in
716
- the future.
717
-
718
- This gives you flexibility without sacrificing performance.
719
-
720
- You may think that this gives us terrible performance: return a value and then
721
- immediately box it up ?! Isn't this pattern the worst of both worlds? Rust is
722
- smarter than that. There is no copy in this code. ` main ` allocates enough room
723
- for the ` box ` , passes a pointer to that memory into ` foo ` as ` x ` , and then
724
- ` foo ` writes the value straight into the ` Box<T> ` .
725
-
726
- This is important enough that it bears repeating: pointers are not for
727
- optimizing returning values from your code. Allow the caller to choose how they
728
- want to use your output.
729
-
730
648
# Creating your own Pointers
731
649
732
650
This part is coming soon.
0 commit comments