Memory Structure of Tuple in Rust

Can some help in explaining/understanding the memory structure of Tuple like

a. where tuple is created heap/stack ?.
b. what happens when a tuple is copied ?.
c. any detailed article/reference would be a great help.

tuples are exactly like structs, specially tuple structs, but they are structural types instead of nominal types (meaning all tuples with the same components are considered the same type, while structs with different names, even the definitions are the same, are different types ). so everything you know about structs applies to tuples.

4 Likes
  1. A tuple is like any other value, and will be placed wherever you put it. For example:
static TUPLE1: (i32, i32) = (1, 2); // in static memory
let tuple2 = (3, 4); // on the stack
let tuple3 = Box::new((5, 6)); // on the heap
  1. When you copy/clone a tuple, it copies/clones each value inside it. Nothing fancy happens.
  2. There aren't any, because there's not much to detail. Here's The Rust Reference.

And if you were curious how tuples are arranged in memory (i.e. which elements go where), it's undefined. The compiler rearranges elements to minimize size.

2 Likes