Rust

E0793 Reference to Packed Field Fix

A packed struct drops the padding that would normally align its fields, so a field can sit at an address its type is not allowed to occupy. Taking a reference to such a field is rejected outright, even if the reference is never used: copy the value out, or reach it through a raw pointer and an unaligned read.

Written November 28, 2026 · Tested with Rust 1.99.1 · 13 min read

By the end of this guide
  • The rule underneath: packed removes the padding that aligns fields
  • The first failure: a reference to the wider field
  • The second failure: a mutable reference fails the same way
  • The scope of the ban: the narrow field is fine

Written November 28, 2026

Tested with Rust 1.99.1

What this guide is

Error E0793 fires when code creates a reference to a field of a packed struct and that field is not aligned the way its type requires. The field itself is not badly typed, and reading it by value is fine. The reference is the problem: a reference promises that the value it points at sits at a properly aligned address, and inside a #[repr(packed)] struct that promise cannot be kept for a field whose type needs more alignment than the packed layout leaves it.

This guide works with one small struct throughout - a u8 followed by a u32, packed - and reproduces the error on a shared reference and on a mutable one. It then shows the scope of the ban, which is narrower than it first looks: a reference to the u8 field of the same struct compiles. After that come the two repairs the compiler's own help text names, copying the field to a local and reading it through a raw pointer, and a closing section on the two layouts that sit next to this one: no packing at all, and packing to 2 bytes instead of 1.

The rule underneath: packed removes the padding that aligns fields

A value of alignment n may only be stored at an address that is a multiple of n. A u32 needs 4-byte alignment; a u8 needs only 1, so it can sit anywhere. In an ordinary struct the compiler inserts padding between fields so that each field lands on an address its type accepts - after a u8, three padding bytes would put the following u32 at offset 4.

The Rust Reference's type layout chapter describes what the packed modifier changes: it lowers the alignment of the struct, #[repr(packed)] without a value means an alignment of 1, and a type packed that way has no inter-field padding. The u32 in the struct below therefore sits at offset 1, an address that is not a multiple of 4. The Reference states the consequence directly: references to unaligned fields are not allowed, because forming one is undefined behavior. The official E0793 error page says the same from the error's side - an unaligned reference to a field of a packed struct or union was created - and adds two points that shape the sections below: using an unsafe block changes nothing, and direct by-value access to the field is fine, because then the compiler knows enough to generate the right kind of access.

The first failure: a reference to the wider field

Here is the struct and the reference that fails:

Rust
#[repr(packed)]
struct Packet {
    kind: u8,
    length: u32,
}

fn main() {
    let p = Packet { kind: 1, length: 258 };
    let r = &p.length;
    println!("{r}");
}
Terminal
$ rustc --edition 2021 p1_fail_ref.rs -o p1_fail_ref.bin
error[E0793]: reference to field of packed struct is unaligned
 --> p1_fail_ref.rs:9:13
  |
9 |     let r = &p.length;
  |             ^^^^^^^^^
  |
  = note: this struct is 1-byte aligned, but the type of this field may require higher alignment
  = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
  = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0793`.

The span covers the whole reference expression, and the two notes carry the diagnosis. The struct is 1-byte aligned - that is what packed set - while the field's type, u32, may require more. And the ban is on creating the reference, not on using it: the second note says the misaligned reference is undefined behavior even if it is never dereferenced, so there is no version of this program that keeps &p.length and stays sound by being careful later. The help line is the repair menu for the whole guide: copy the field out, or go through a raw pointer and unaligned access.

The second failure: a mutable reference fails the same way

Mutability does not change the alignment arithmetic, so a mutable reference to the same field is rejected with the same error:

Rust
#[repr(packed)]
struct Packet {
    kind: u8,
    length: u32,
}

fn main() {
    let mut p = Packet { kind: 1, length: 258 };
    let r = &mut p.length;
    *r = 259;
    let length = p.length;
    println!("{length}");
}
Terminal
$ rustc --edition 2021 p3_fail_mut_ref.rs -o p3_fail_mut_ref.bin
error[E0793]: reference to field of packed struct is unaligned
 --> p3_fail_mut_ref.rs:9:13
  |
9 |     let r = &mut p.length;
  |             ^^^^^^^^^^^^^
  |
  = note: this struct is 1-byte aligned, but the type of this field may require higher alignment
  = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
  = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0793`.

Same header, same notes, same help; only the underlined expression is longer, because it now includes mut. Writing through a packed field is not ruled out by this - it just cannot go through a reference. The raw-pointer fix below has a writing counterpart, named in the help text: form the pointer with the mutable form of the macro and store with the unaligned write instead of dereferencing the pointer.

The scope of the ban: the narrow field is fine

The error names a field, not the whole struct, and the distinction shows on the other field of the same packed struct. A u8 requires 1-byte alignment, and every address satisfies that, so a reference to kind is always aligned enough:

Rust
#[repr(packed)]
struct Packet {
    kind: u8,
    length: u32,
}

fn main() {
    let p = Packet { kind: 1, length: 258 };
    let r = &p.kind;
    let length = p.length;
    println!("{r} {length}");
}
Terminal
$ rustc --edition 2021 p2_scope_u8_ref.rs -o p2_scope_u8_ref.bin
$ ./p2_scope_u8_ref.bin
1 258

This program compiles with no error and no warning, and it prints both fields: kind through the reference, length as a by-value copy read on the line before the print. The E0793 page makes the same point with its own example, noting that a reference to a field that will always be sufficiently aligned is safe. So when E0793 fires, the question to ask is per field - what alignment does this field's type need, and what alignment did the packing leave it - not whether the struct is packed at all.

The first fix: copy the field to a local

The simplest repair stops taking the reference. Reading a packed field by value is allowed, because the compiler generates an access that copes with the field's actual address, and the copy it produces is an ordinary, properly aligned local that code can reference, pass around, and print as much as it likes:

Rust
#[repr(packed)]
struct Packet {
    kind: u8,
    length: u32,
}

fn main() {
    let p = Packet { kind: 1, length: 258 };
    let kind = p.kind;
    let length = p.length;
    println!("{kind} {length}");
}
Terminal
$ rustc --edition 2021 p4_fix_copy.rs -o p4_fix_copy.bin
$ ./p4_fix_copy.bin
1 258

Both fields are copied here, and the print works on the copies. This is the right fix whenever the code only needs the value - to print it, compare it, compute with it, or hand it to a function by value. Its limit is just as plain: a copy is a snapshot. Writing to the local does not write back into the struct, so code that must change the field in place, or must hand another function a pointer to the field itself, needs the second fix instead.

The second fix: a raw pointer and an unaligned read

When the code genuinely needs to reach the field where it lies, the route is a raw pointer, because a raw pointer carries no alignment promise. The one thing to avoid is forming that pointer through a reference - casting &p.length to a pointer would create the banned reference on the way. The addr_of! macro exists for exactly this step: the addr_of! documentation says it creates a raw pointer to a place without creating an intermediate reference, and its own worked example is a packed struct read this way. Current docs describe the macro as equivalent to the &raw const syntax. Reading the value back then has to be explicitly unaligned, inside an unsafe block:

Rust
#[repr(packed)]
struct Packet {
    kind: u8,
    length: u32,
}

fn main() {
    let p = Packet { kind: 1, length: 258 };
    let kind = p.kind;
    let ptr = std::ptr::addr_of!(p.length);
    let length = unsafe { ptr.read_unaligned() };
    println!("{kind} {length}");
}
Terminal
$ rustc --edition 2021 p5_fix_raw_pointer.rs -o p5_fix_raw_pointer.bin
$ ./p5_fix_raw_pointer.bin
1 258

Two halves make this sound where a plain dereference would not be. The pointer is formed without a reference, and the load uses the unaligned read rather than *ptr - the compiler's help text warns that loads and stores through a dereferenced raw pointer must still be properly aligned. The writing counterpart follows the same pattern with the mutable form of the macro and the unaligned write. Reach for this fix only when a copy will not do; it puts the alignment reasoning on the programmer, which is what the unsafe block is marking.

Neighboring cases: no packing, and packing to 2

Two nearby layouts show that the error tracks alignment, not the word packed by itself. First, the identical struct with no attribute at all. Without packing, padding returns, the u32 lands at an aligned offset, and the very reference that failed in the first section is accepted:

Rust
struct Packet {
    kind: u8,
    length: u32,
}

fn main() {
    let p = Packet { kind: 1, length: 258 };
    let r = &p.length;
    println!("{} {r}", p.kind);
}
Terminal
$ rustc --edition 2021 p6_neighbour_unpacked.rs -o p6_neighbour_unpacked.bin
$ ./p6_neighbour_unpacked.bin
1 258

Second, packing with a bound instead of packing fully. #[repr(packed(2))] caps the struct's alignment at 2 rather than 1, which is still short of the 4 a u32 requires, so the same reference still fails - with the note's number changed to match:

Rust
#[repr(packed(2))]
struct Packet {
    kind: u8,
    length: u32,
}

fn main() {
    let p = Packet { kind: 1, length: 258 };
    let r = &p.length;
    println!("{r}");
}
Terminal
$ rustc --edition 2021 p7_neighbour_packed2.rs -o p7_neighbour_packed2.bin
error[E0793]: reference to field of packed struct is unaligned
 --> p7_neighbour_packed2.rs:9:13
  |
9 |     let r = &p.length;
  |             ^^^^^^^^^
  |
  = note: this struct is at most 2-byte aligned, but the type of this field may require higher alignment
  = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
  = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0793`.

Read together, the three layouts give the whole rule. The reference is allowed exactly when the field's placement meets its type's alignment: always in the unpacked struct, never for the u32 at either packing level tried here, and always for the u8, whose requirement of 1 is met everywhere. Loosening the packing helps only once it reaches the field's own requirement.

Decision summary

  • The header says "reference to field of packed struct is unaligned" (E0793) and the note gives the struct's alignment as 1 byte: a reference was taken to a field whose type needs more alignment than the packed layout gives it - copy the field out, or use the raw-pointer route.
  • The failing expression is a mutable reference: mutability changes nothing - the same error, the same two fixes, with the unaligned write as the writing counterpart of the unaligned read.
  • Only some fields of a packed struct trigger the error: check each field's own alignment requirement - a u8 field can be referenced freely, a wider field after it usually cannot.
  • The code only reads the value, for printing, comparing, or computing: copy the field to a local and work on the copy.
  • The code must read or write the field in place, or pass its address onward: form a raw pointer with addr_of!, never through a reference, and use the unaligned read or write - never a plain dereference.
  • The same reference compiles once the packing attribute is removed, or still fails under a packed bound below the field's alignment: the layout is the cause, and raising the bound only helps once it reaches what the field's type requires.

Checklist

  • No reference, shared or mutable, is taken to a packed field whose type needs more alignment than the packed layout leaves it.
  • A reference to a field whose type needs only 1-byte alignment, such as a u8, is left as it is - it was never part of the error.
  • Fields that are only read are copied to locals first, and the rest of the code works on the copies.
  • A raw pointer to a packed field is formed with addr_of! or its mutable counterpart, never by casting a reference, and is read or written only with the unaligned operations.
  • No packed field is loaded or stored through a plain dereference of a raw pointer.
  • The fixed program compiles, runs, and prints the field values the original code was written to print.
  • E0512 Transmute Size Mismatch Fix - the other layout error in this set: a transmute between types of different sizes is rejected outright, where here the type is fine and only the field's placement is at fault.
  • E0605 Invalid Cast Fix - the cast-side companion: casting a reference to a pointer is a natural first instinct for packed fields, and this guide covers which casts Rust allows at all - though the pointer here must be formed without a reference in the first place.
  • E0133 Unsafe Call Fix | Veldica - the neighboring unsafe error: the raw-pointer fix reads inside an unsafe block, and E0133 is what fires when an unsafe operation is used without one.

Sources

  • Rust, Error codes index: E0793 - the definition of the error: an unaligned reference to a field of a packed struct or union was created; packed removes padding so fields can sit at unaligned addresses, an unsafe block changes nothing, and the stated repairs are a copy of the field or raw pointers with unaligned accesses - the error, its scope, and both fixes in this guide.
  • The Rust Reference, Type layout - the layout rules underneath: a value of alignment n belongs only at addresses that are multiples of n, packed with no value sets an alignment of 1 and a type packed that way has no inter-field padding, each field's effective alignment is the smaller of the packed bound and its own, and references to unaligned fields are not allowed - the rule section and the neighboring packed-bound case.
  • Rust, std::ptr::addr_of! - the pointer-forming step of the second fix: the macro creates a raw pointer to a place without creating an intermediate reference, casting a reference to a pointer would create that reference first, and its worked example reads a field of a packed struct through the pointer with the unaligned read.

Keep Exploring