Do references take up memory?

Viewed 103

I am currently a month or two into learning Rust and wanted to make a small program to learn it which consists of objects that have a struct containing two u8’s. My question is, whenever that struct is asked-for immutably, would cloning/copying it be more memory efficient than referencing to it since the reference would presumably take up 8 bytes on a 64-bit system, while the struct itself only uses 2 bytes?

1 Answers

A struct consisting of two u8's is basically a u16. Since u16 implements Copy, you probably want to make your struct implement Copy as well. For such small types a reference will always be larger than the type itself.

#[derive(Copy, Clone)]
struct U16 {
    a: u8,
    b: u8,
}
Related