offset_of_enum - The Rust Unstable Book (original) (raw)
Keyboard shortcuts
Press ← or → to navigate between chapters
Press S or / to search in the book
Press ? to show this help
Press Esc to hide this help
The Rust Unstable Book
offset_of_enum
The tracking issue for this feature is: #120141
When the offset_of_enum
feature is enabled, the offset_of! macro may be used to obtain the offsets of fields of enum
s; to express this, enum
variants may be traversed as if they were fields. Variants themselves do not have an offset, so they cannot appear as the last path component.
#![allow(unused)]
#![feature(offset_of_enum)]
fn main() {
use std::mem;
#[repr(u8)]
enum Enum {
A(u8, u16),
B { one: u8, two: u16 },
}
assert_eq!(mem::offset_of!(Enum, A.0), 1);
assert_eq!(mem::offset_of!(Enum, B.two), 2);
assert_eq!(mem::offset_of!(Option<&u8>, Some.0), 0);
}