Rust errors / Attributes, macros and features
Rust compiler error · checked with rustc 1.99.0
Rust error E0795: `Some` is an enum variant; expected field at end of `offset_of`
What it meansInvalid argument for the offset_of! macro.
What rustc prints
error[E0795]: `Some` is an enum variant; expected field at end of `offset_of`
--> main.rs:4:42
|
4 | let x = std::mem::offset_of!(Option<u8>, Some);
| ^^^^ enum variantReal output of rustc 1.99.0 compiling the official example below (2021 edition). Your names and line numbers will differ; the first line is the part to match.
The compiler team's explanation
Invalid argument for the offset_of! macro.
Erroneous code example:
#![feature(offset_of_enum)]
let x = std::mem::offset_of!(Option<u8>, Some);
The offset_of! macro gives the offset of a field within a type. It can navigate through enum variants, but the final component of its second argument must be a field and not a variant.
The offset of the contained u8 in the Option<u8> can be found by specifying the field name 0:
#![feature(offset_of_enum)]
let x: usize = std::mem::offset_of!(Option<u8>, Some.0);
The discriminant of an enumeration may be read with core::mem::discriminant, but this is not always a value physically present within the enum.
Further information about enum layout may be found at https://rust-lang.github.io/unsafe-code-guidelines/layout/enums.html.
From rustc --explain E0795 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.
Related errors
Up next
Often hit nextE0658: is unstableA related error from the same family.Read it next →Did this answer your question?