diff --git a/src/std/box.md b/src/std/box.md index 64856d7aa7..8d45e1a44e 100644 --- a/src/std/box.md +++ b/src/std/box.md @@ -1,9 +1,9 @@ # Box, stack and heap All values in Rust are stack allocated by default. Values can be *boxed* -(allocated in the heap) by creating a `Box`. A box is a smart pointer to a +(allocated on the heap) by creating a `Box`. A box is a smart pointer to a heap allocated value of type `T`. When a box goes out of scope, its destructor -is called, the inner object is destroyed, and the memory in the heap is freed. +is called, the inner object is destroyed, and the memory on the heap is freed. Boxed values can be dereferenced using the `*` operator; this removes one layer of indirection. @@ -29,7 +29,7 @@ fn origin() -> Point { } fn boxed_origin() -> Box { - // Allocate this point in the heap, and return a pointer to it + // Allocate this point on the heap, and return a pointer to it Box::new(Point { x: 0.0, y: 0.0 }) } @@ -54,22 +54,22 @@ fn main() { // Double indirection let box_in_a_box: Box> = Box::new(boxed_origin()); - println!("Point occupies {} bytes in the stack", + println!("Point occupies {} bytes on the stack", mem::size_of_val(&point)); - println!("Rectangle occupies {} bytes in the stack", + println!("Rectangle occupies {} bytes on the stack", mem::size_of_val(&rectangle)); - // box size = pointer size - println!("Boxed point occupies {} bytes in the stack", + // box size == pointer size + println!("Boxed point occupies {} bytes on the stack", mem::size_of_val(&boxed_point)); - println!("Boxed rectangle occupies {} bytes in the stack", + println!("Boxed rectangle occupies {} bytes on the stack", mem::size_of_val(&boxed_rectangle)); - println!("Boxed box occupies {} bytes in the stack", + println!("Boxed box occupies {} bytes on the stack", mem::size_of_val(&box_in_a_box)); // Copy the data contained in `boxed_point` into `unboxed_point` let unboxed_point: Point = *boxed_point; - println!("Unboxed point occupies {} bytes in the stack", + println!("Unboxed point occupies {} bytes on the stack", mem::size_of_val(&unboxed_point)); } ``` \ No newline at end of file