@@ -841,76 +841,6 @@ as in this example that unpacks the first value from a tuple and returns it.
841
841
fn first((value, _): (int, float)) -> int { value }
842
842
~~~
843
843
844
-
845
- # The Rust memory model
846
-
847
- At this junction, let's take a detour to explain the concepts involved
848
- in Rust's memory model. We've seen some of Rust's pointer sigils (` @ ` ,
849
- ` ~ ` , and ` & ` ) float by in a few examples, and we aren't going to get
850
- much further without explaining them. Rust has a very particular
851
- approach to memory management that plays a significant role in shaping
852
- the subjective experience of programming in the
853
- language. Understanding the memory landscape will illuminate several
854
- of Rust's unique features as we encounter them.
855
-
856
- Rust has three competing goals that inform its view of memory:
857
-
858
- * Memory safety: Memory that the Rust language can observe must be
859
- guaranteed to be valid. Under normal circumstances, it must be
860
- impossible for Rust to trigger a segmentation fault or leak memory.
861
- * Performance: High-performance low-level code must be able to use
862
- a number of different allocation strategies. Tracing garbage collection must be
863
- optional and, if it is not desired, memory safety must not be compromised.
864
- Less performance-critical, high-level code should be able to employ a single,
865
- garbage-collection-based, heap allocation strategy.
866
- * Concurrency: Rust code must be free of in-memory data races. (Note that other
867
- types of races are still possible.)
868
-
869
- ## How performance considerations influence the memory model
870
-
871
- Most languages that offer strong memory safety guarantees rely on a
872
- garbage-collected heap to manage all of the objects. This approach is
873
- straightforward both in concept and in implementation, but has
874
- significant costs. Languages that follow this path tend to
875
- aggressively pursue ways to ameliorate allocation costs (think the
876
- Java Virtual Machine). Rust supports this strategy with _ managed
877
- boxes_ : memory allocated on the heap whose lifetime is managed
878
- by the garbage collector.
879
-
880
- By comparison, languages like C++ offer very precise control over
881
- where objects are allocated. In particular, it is common to allocate them
882
- directly on the stack, avoiding expensive heap allocation. In Rust
883
- this is possible as well, and the compiler uses a [ clever _ pointer
884
- lifetime analysis_ ] [ borrow ] to ensure that no variable can refer to stack
885
- objects after they are destroyed.
886
-
887
- [ borrow ] : tutorial-borrowed-ptr.html
888
-
889
- ## How concurrency considerations influence the memory model
890
-
891
- Memory safety in a concurrent environment involves avoiding race
892
- conditions between two threads of execution accessing the same
893
- memory. Even high-level languages often require programmers to make
894
- correct use of locking to ensure that a program is free of races.
895
-
896
- Rust starts from the position that memory cannot be shared between
897
- tasks. Experience in other languages has proven that isolating each
898
- task's heap from the others is a reliable strategy and one that is
899
- easy for programmers to reason about. Heap isolation has the
900
- additional benefit that garbage collection must only be done
901
- per-heap. Rust never "stops the world" to reclaim memory.
902
-
903
- Complete isolation of heaps between tasks would, however, mean that
904
- any data transferred between tasks must be copied. While this is a
905
- fine and useful way to implement communication between tasks, it is
906
- also very inefficient for large data structures. To reduce the amount
907
- of copying, Rust also uses a global _ exchange heap_ . Objects allocated
908
- in the exchange heap have _ ownership semantics_ , meaning that there is
909
- only a single variable that refers to them. For this reason, they are
910
- referred to as _ owned boxes_ . All tasks may allocate objects on the
911
- exchange heap, then transfer ownership of those objects to other
912
- tasks, avoiding expensive copies.
913
-
914
844
# Boxes and pointers
915
845
916
846
Many modern languages have a so-called "uniform representation" for
0 commit comments