Question about memory consumption of a reference to a constant

⚓ Rust    📅 2026-03-07    👤 surdeus    👁️ 1      

surdeus

The question is how much memory consumption will SMALL_SLICE cause in the following code? Will the compiler optimize out the unread part?

const ARRAY_100KB: [u8; 102400] = [0; 102400];
static SMALL_SLICE: &[u8] = ARRAY_100KB.split_at(5).0;

Google Gemini said 21 bytes in total, which combined by 16 byte(fat pointer) + 5 byte(underlying data), but I'm not sure.

BTW, is that a better way to get a range of elements of a slice in constant environment?

7 posts - 3 participants

Read full topic

🏷️ Rust_feed