Email me

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 variant

Real 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.

E0795Attributes, macros and features
Activein rustc 1.99.0
5related errors linked below

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

All 518 Rust error codes · Rust crate data · Glossary

Up next

Often hit nextE0658: is unstableA related error from the same family.Read it next →

Did this answer your question?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ