Preservation of the pointer part of empty slice

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

surdeus

Consider the following toy example:

fn main() {
    let buffer: [u8; 1] = [b'a'];
    let empty_slice: &[u8] = &buffer[0..0]; 
    let empty_slice2: &[u8] = &buffer[1..1];
    println!("same ptr: {}", empty_slice.as_ptr() == empty_slice2.as_ptr())
}

Can Rust compiler optimize this such that it prints true? I.e. can the compiler assume that all pointer parts of empty slices are equivalent and can be replaced with each other? Or in other way, one cannot assume that the pointer part of the empty slice is preserved, can one?

3 posts - 2 participants

Read full topic

🏷️ Rust_feed