Rust
Cannot Transmute Zero-Sized Type E0591 Fix
E0591 is the compiler refusing a transmute whose source is a function item. A named function does not have the type of a function pointer: it has its own unique zero-sized type, so there are no bytes for a transmute to reinterpret as a pointer-sized target. The diagnostic names both types and ends with one instruction - cast with `as` to a pointer instead. This guide shows the error on the target shapes that draw it, the repairs that cast first or skip the transmute entirely, and the neighboring code a different zero-sized source draws instead.
Written December 1, 2026 · Tested with Rust 1.99.1 · 14 min read
- The rule underneath: a function item is zero-sized
- How to read the diagnostic
- The first failure: a function item transmuted to a function pointer
- The same error across the target shapes
Written December 1, 2026
Tested with Rust 1.99.1
What this guide is
Error E0591 is about the source of a transmute, not its target value. The error codes index explains that for a function declaration foo, the type of foo is not fn(S) as one might expect: it is a unique, zero-sized marker type specific to that one function. A function pointer such as fn(S) is pointer-sized and names no particular function. Transmuting the item straight into the pointer asks the compiler to reinterpret zero bytes as a full pointer, and it refuses.
The practical payoff is that the repair is a cast, not a redesign. A function item coerces to a function pointer for free, and it can be cast with as to a pointer or an integer. Once the value actually is pointer-sized, a transmute that still has work to do - changing the argument types for a foreign callback, for example - has real bytes to work with. The neighboring code in this area is E0512, the general size-mismatch transmute error; it is covered as a boundary below, because a zero-sized struct draws E0512, not E0591, and telling the two apart is most of the diagnosis.
Build every program in this guide with rustc --edition 2021. In the compiler transcripts below, the file names are the bare names shown on the command line.
The rule underneath: a function item is zero-sized
The index gives the reason in one sentence: the type of a function item is zero-sized, and the target type - a function pointer - is not zero-sized. A transmute reinterprets the bits of its source as its target, so source and target must occupy the same number of bits. A function item occupies none: it carries its identity in its type, not in any stored value. Calling the item works because the compiler knows statically which function the type names; there is no address stored anywhere to copy into a pointer.
That is also why the failure is specific to items. The moment the item is coerced or cast into a function pointer, the value becomes an ordinary address with pointer size, and the same transmute is accepted. And it is why other zero-sized values behave differently: a unit struct or () transmuted to a differently sized type is a plain size mismatch and draws E0512, as the boundary section below shows. E0591 is reserved for the function-item source, where the compiler can name the actual fix - a cast.
How to read the diagnostic
In the probed set, an E0591 failure is a single error with two notes and one help line. The header states the rule: "can't transmute zero-sized type". The span underlines the whole transmute(...) call. The first note names the source type, printing the item's signature with the function's name in braces - extern "C" fn(Box<i32>) {foo} - which is the zero-sized marker type made visible. The second note names the target type. The help line is the repair in short form: "cast with as to a pointer instead".
Read the two notes as the whole diagnosis: if the source note ends in {name}, the value being transmuted is a function item, whatever the surrounding code intended. If your diagnostic instead reports bit counts for both types, you are in E0512 territory, covered below, and no cast of a function is involved.
The first failure: a function item transmuted to a function pointer
The smallest shape is the index's foreign-function pattern - a callback whose argument is a boxed integer, transmuted into the raw-pointer signature the receiving function expects:
extern "C" fn foo(userdata: Box<i32>) {
println!("{}", *userdata);
}
fn callback(_: extern "C" fn(*mut i32)) {}
fn main() {
use std::mem::transmute;
unsafe {
let f: extern "C" fn(*mut i32) = transmute(foo);
callback(f);
}
}
error[E0591]: can't transmute zero-sized type
--> fail_fn_item.rs:8:42
|
8 | let f: extern "C" fn(*mut i32) = transmute(foo);
| ^^^^^^^^^
|
= note: source type: extern "C" fn(Box<i32>) {foo}
= note: target type: extern "C" fn(*mut i32)
= help: cast with `as` to a pointer instead
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0591`.
Nothing is wrong with the target signature, with callback, or with calling foo directly. The one defect is the source: foo in this position is the item, with its zero-sized type, not the pointer the transmute needs. The index is explicit that this pattern is incorrect for exactly that reason, and its preferred repair - changing the original declaration to match the expected signature - is shown running below, alongside the cast-first repair for declarations that cannot change.
The same error across the target shapes
The target does not have to be a function pointer. Transmuting the item into an integer - the other half of many foreign-function idioms - fails identically, because the defect is the zero-sized source, not the destination:
fn foo(x: i32) -> i32 { x + 1 }
fn main() {
use std::mem::transmute;
unsafe {
let n: usize = transmute(foo);
println!("{}", n);
}
}
error[E0591]: can't transmute zero-sized type
--> fail_fn_item_usize.rs:5:24
|
5 | let n: usize = transmute(foo);
| ^^^^^^^^^
|
= note: source type: fn(i32) -> i32 {foo}
= note: target type: usize
= help: cast with `as` to a pointer instead
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0591`.
A pointer to a function pointer - the *mut fn() shape the index notes it has observed in practice - fails the same way. The target is still pointer-sized and the source is still zero-sized:
fn foo() {}
fn main() {
use std::mem::transmute;
unsafe {
let p: *mut fn() = transmute(foo);
println!("{:p}", p);
}
}
error[E0591]: can't transmute zero-sized type
--> fail_fn_ptr_mut.rs:5:28
|
5 | let p: *mut fn() = transmute(foo);
| ^^^^^^^^^
|
= note: source type: fn() {foo}
= note: target type: *mut fn()
= help: cast with `as` to a pointer instead
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0591`.
In every probed failing shape the diagnostic is identical apart from the target note: a function pointer, a usize, and a *mut fn(). The failing family is one shape - a transmute whose source expression is a bare function item.
The repair: cast the item to a function pointer first
The index's first repair keeps the transmute but changes its source: cast the item to a function pointer with as, so the transmute receives a pointer-sized value whose argument type it can then reinterpret. Here the cast-first value is called with a raw pointer made from a box, and foo runs:
extern "C" fn foo(userdata: Box<i32>) {
println!("{}", *userdata);
}
fn main() {
use std::mem::transmute;
unsafe {
let f: extern "C" fn(*mut i32) = transmute(foo as extern "C" fn(_));
let boxed = Box::new(41);
f(Box::into_raw(boxed));
}
}
41
The output is foo printing the integer it received, 41 - the transmuted pointer was called and reached the right function. The cast is the whole fix: foo as extern "C" fn(_) is the coercion to a function pointer written explicitly, and the transmute after it only changes the argument type from Box<i32> to *mut i32, two pointer-sized types. That second step is still an unchecked reinterpretation, which is why this repair belongs behind the same care as any transmute - but it is the shape the index prescribes when the declaration itself cannot change.
The repair: cast through a pointer to an integer first
The index's second repair casts the item to usize before the transmute. Written with a pointer as the intermediate step - cast to a raw pointer first, then to the integer - it compiles with no warning and runs the same way:
extern "C" fn foo(userdata: Box<i32>) {
println!("{}", *userdata);
}
fn main() {
use std::mem::transmute;
unsafe {
let f: extern "C" fn(*mut i32) = transmute(foo as *const () as usize);
let boxed = Box::new(43);
f(Box::into_raw(boxed));
}
}
43
The output is again foo printing its argument, here 43. The index also shows the direct form, transmute(foo as usize): it compiles and runs, but the current compiler warns on it - "direct cast of function item into an integer" - and suggests the pointer intermediate used here. Either form rests on the same fact: a cast can manufacture the address a function item does not store, and a transmute cannot.
The repair: drop the transmute and let the item coerce
Most code that transmutes a function item does not need a transmute at all. Where the target is the function's own signature as a pointer, the item coerces on assignment - the index's opening example is exactly this, and it runs:
struct S;
fn foo(_: S) {
println!("foo called");
}
fn main() {
let x: fn(S) = foo;
x(S);
println!("coerced and called");
}
foo called
coerced and called
The first line is the call through the pointer x reaching foo; the second confirms control returned to main. No cast and no unsafe block appear, because nothing is being reinterpreted - the coercion from item to pointer of the same signature is a conversion the language defines.
The repair the index prefers: match the declaration
For the foreign-callback pattern, the index names its preferred option first: change the original function declaration to match the expected signature, and do any conversion inside the function body. When foo is declared with the raw-pointer parameter from the start, no transmute and no cast are needed anywhere - the item coerces straight into the receiving function's parameter:
extern "C" fn foo(userdata: *mut i32) {
println!("{}", unsafe { *userdata });
}
fn callback(f: extern "C" fn(*mut i32)) {
let mut v = 42;
f(&mut v as *mut i32);
}
fn main() {
callback(foo);
}
42
The output is the value callback passed through the pointer, 42, printed by foo. Reach for this shape whenever the function is yours to declare: the signature states the contract the foreign side actually uses, and the unsafe surface shrinks to the dereference inside the body. The cast-first repairs above are for the remaining case - a declaration fixed by someone else's code or by an existing binary interface.
The boundary: a zero-sized struct draws E0512, not E0591
Zero-sized alone does not draw E0591. Transmuting a value of an ordinary zero-sized type - a unit struct - into a differently sized type is a plain size mismatch, and the compiler reaches for the general code:
struct Marker;
fn main() {
use std::mem::transmute;
unsafe {
let n: u8 = transmute(Marker);
println!("{}", n);
}
}
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
--> fail_zst_struct.rs:5:21
|
5 | let n: u8 = transmute(Marker);
| ^^^^^^^^^
|
= note: source type: `Marker` (0 bits)
= note: target type: `u8` (8 bits)
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0512`.
The contrast is the fastest way to separate the two codes in real output. E0512 reports both sizes in bits - here 0 bits against 8 bits - and offers no help line, because there is no standard conversion it can point at. E0591 reports no bit counts at all: it names the item type with its {foo} marker and prescribes the cast. The unit value () behaves like the struct - transmuting it to a u32 also draws E0512, with () reported at 0 bits. So "zero-sized type" in the E0591 header means, in practice, a function item; every other zero-sized source takes the E0512 path.
The boundary: an ordinary size mismatch is E0512
The same split holds with no zero-sized type anywhere. Transmuting between two ordinary types of different sizes is E0512, with the bit counts stated for both:
fn main() {
use std::mem::transmute;
unsafe {
let n: u16 = transmute(1u8);
println!("{}", n);
}
}
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
--> boundary_size_mismatch.rs:4:22
|
4 | let n: u16 = transmute(1u8);
| ^^^^^^^^^
|
= note: source type: `u8` (8 bits)
= note: target type: `u16` (16 bits)
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0512`.
The repair there is different in kind: use types of the same size, or use the expected type directly, as the E0512 index entry puts it - there is no function and no cast of one to reach for. The E0512 guide covers the size-mismatch side in full. If your transmute error shows bit counts, stay there; if it shows a {name} item type and the cast help line, you are in this guide.
Fixes in reach order
First, read the source note. If it ends in {name} - a signature followed by the function's name in braces - the source is a function item and the code is E0591. If the notes give bit counts instead, the code is E0512 and the fix is about sizes, not functions.
Second, ask whether the transmute needs to exist. If the target is a function pointer with the function's own signature, delete the transmute and let the item coerce on assignment or argument passing - no cast, no unsafe. If the function is yours to declare and the target signature is the one callers use, change the declaration to match it, as the index prefers.
Third, when the signature genuinely has to change across the boundary - the foreign-callback case - keep the transmute but cast the item first: to a function pointer with as extern "C" fn(_), or through a raw pointer to an integer and back. In the probed set, each of these took its failing shape to compiling and running with no other change.
Last, check the target form before re-reading the source. A *mut fn() target usually signals a confusion the index calls out directly: the intention is typically to describe a function pointer, and plain fn() suffices for that - *mut fn() is a pointer to a function pointer, one level further out than most code means.
Decision summary
E0591, in this guide's probed set, fired exactly when a transmute took a bare function item as its source: transmuted to a function pointer of a different signature, to a usize, and to a *mut fn(). Every diagnostic was a single error headed "can't transmute zero-sized type", underlining the transmute(...) call, with a source note naming the item type ending in {foo}, a target note, and the help line "cast with as to a pointer instead". Compiling and running were: the item cast to a function pointer before the transmute, the item cast through a raw pointer to an integer before the transmute, the item coerced to a function pointer with no transmute, and a declaration rewritten to the expected signature so the item passed directly. A unit struct transmuted to a u8 and a u8 transmuted to a u16 both drew E0512 with bit counts instead.
Checklist
- Read the source note: a signature ending in
{name}is a function item - E0591; bit counts are E0512. - A function item is zero-sized - it has no stored address for a transmute to reinterpret.
- If the target is the function's own pointer signature, drop the transmute and let the item coerce.
- If the declaration is yours, match it to the expected signature and convert inside the body.
- If the signature must change across the boundary, cast the item to a function pointer before transmuting.
- Cast to an integer through a raw pointer (
as *const () as usize), not directly from the item. - A
*mut fn()is a pointer to a function pointer - check whether plainfn()is what the code means.
Related guides on this site
- E0412 Cannot Find Type Fix
- E0509 Cannot Move Out Fix
- E0091 Unused Type Parameter Fix
- E0118 Inherent Impl on Non-Nominal Type Fix
Sources
- Rust error codes index, E0591 - a function item has a unique zero-sized marker type rather than the function pointer type; the item coerces to a function pointer; the incorrect pattern of transmuting the item to convert the types of the function arguments, common in foreign-function code; and the repairs - change the original declaration to match the expected signature and cast in the body, or cast the item to a function pointer, or through an integer, before calling transmute; plus the note on
*mut fn()targets. - Rust error codes index, E0512 - a transmute with two differently sized types was attempted; use types of the same size or use the expected type directly.
Keep Exploring
The full Rust hub \u2014 ownership, structs and enums, async, and measured comparisons.
Every guide on the site in one list, grouped by topic.
Every browser-only checker and verifier on the site.
The C++, Python, Sysadmin, Rust, Go, and TypeScript hubs.