Skip to content

Instantly share code, notes, and snippets.

@mickvangelderen
Created December 5, 2025 20:06
Show Gist options
  • Select an option

  • Save mickvangelderen/e91004778f897cfceceea3a378482000 to your computer and use it in GitHub Desktop.

Select an option

Save mickvangelderen/e91004778f897cfceceea3a378482000 to your computer and use it in GitHub Desktop.
Explore the address of zero-sized types in Rust
struct ZST;
fn main() {
let mark1 = 1;
let z1 = ZST;
let mark2 = 2;
let z2 = ZST;
let mark3 = 3;
static Z1: ZST = ZST;
static Z2: ZST = ZST;
println!("expression (ptr::dangling())");
println!("&ZST: {:?}", &ZST as *const _);
println!("&ZST: {:?}", &ZST as *const _);
println!();
println!("stack (different random addresses)");
println!("&z1: {:?}", &z1 as *const _);
println!("&z2: {:?}", &z2 as *const _);
println!();
println!("static (same random address)");
println!("&Z1: {:?}", &Z1 as *const _);
println!("&Z2: {:?}", &Z2 as *const _);
println!();
println!("marks (overlap with &z1, &z2)");
println!("&mark1: {:?}", &mark1 as *const _);
println!("&mark2: {:?}", &mark2 as *const _);
println!("&mark3: {:?}", &mark3 as *const _);
println!();
}
@mickvangelderen
Copy link
Author

playground

example output:

expression (ptr::dangling())
&ZST:   0x1
&ZST:   0x1

stack (different random addresses)
&z1:    0x7ffff440ca4b
&z2:    0x7ffff440ca53

static (same random address)
&Z1:    0x601113134ddf
&Z2:    0x601113134ddf

marks (overlap with &z1, &z2)
&mark1: 0x7ffff440ca44
&mark2: 0x7ffff440ca4c
&mark3: 0x7ffff440ca54

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment