Created
December 5, 2025 20:06
-
-
Save mickvangelderen/e91004778f897cfceceea3a378482000 to your computer and use it in GitHub Desktop.
Explore the address of zero-sized types in Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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!(); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
playground
example output: