The memory size of the struct _Add here would be the size of _a and _b combined, in other words for any type T and length of n, [T; n] has the size of n * size_of::<T>(), and since here T is u8 and its size is just 1 byte; hence [u8; n] is just n bytes. Using this; the size of _Add would be the size of [u8; A] (A bytes) and the size of [u8; B] (B bytes); will result in total is A+B bytes.
Can you explain the trick with size?
The memory size of the struct
_Add
here would be the size of_a
and_b
combined, in other words for any typeT
and length ofn
,[T; n]
has the size ofn * size_of::<T>()
, and since hereT
isu8
and its size is just1
byte; hence[u8; n]
is justn
bytes. Using this; the size of_Add
would be the size of[u8; A]
(A bytes) and the size of[u8; B]
(B bytes); will result in total isA+B
bytes.Yes but this is where I'm lost. Wouldn't be the returned value 2 in every case?
No, in the test case,
A = 40
means[0u8; 40]
that also means40 bytes
, andB = 2
which is[0u8; 2]
that's 2 bytes, which is total of42
bytes.Ah indeed, thanks! I misunderstood the syntax.
edit : that's a really smart solution btw, thanks for sharing