Rust

E0774 Derive on Non-Type Item Fix

A derive attribute generates a trait implementation for one data type, so it belongs on that type's definition - a struct, an enum, or a union - and nowhere else. On a function, a trait, an impl block, or any other item it is E0774. This guide works the positions that draw the error, matches each one to its repair, and separates the nearby shapes that draw a different, un-coded error instead.

Written November 29, 2026 · Tested with Rust 1.99.1 · 20 min read

By the end of this guide
  • The rule underneath: derive generates an impl for one data type
  • How to read the diagnostic
  • The first failure: derive on a function
  • The second failure: derive on a trait

Written November 29, 2026

Tested with Rust 1.99.1

What this guide is

Error E0774 is a derive attribute placed on an item that is not a struct, a union, or an enum. The error codes index states it in one line - "derive was applied on something which is not a struct, a union or an enum" - and its erroneous example is a #[derive(Clone)] attribute on an associated type inside a trait, marked "error!", repaired in its working example by putting #[derive(Clone)] on a struct instead, marked "ok!".

A derive does one job: it generates a trait implementation for a data type. #[derive(Debug)] on a struct asks the compiler to write the Debug implementation for that struct, from that struct's fields. An item with no fields and no variants - a function, a trait definition, a constant - has nothing for the generated implementation to be built from, and most such items are not types at all. The defect is never in the trait named inside the attribute; Debug, Clone, and the other built-in derives are all valid names. It is in the item the attribute is attached to.

The forms in this guide each reproduce E0774 with a small, complete program: a function, a trait, an impl block, a type alias, a constant, a static, a module, a use declaration, and an associated type, each followed by its repair. The traps then cover the shapes that sit next to this error: a derive on a trait implementation, two stacked derive attributes on one function, a derive on an enum variant or a struct field, and a derive that names a macro which does not exist.

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: derive generates an impl for one data type

The Rust Reference defines the attribute directly: the derive attribute invokes one or more derive macros, "allowing new items to be automatically generated for data structures", and it states the placement rule in one sentence - "The derive attribute may only be applied to structs, enums, and unions." It also lists the built-in derives the standard library provides: Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, and PartialOrd. Every one of them generates an implementation of its trait for the annotated type, built from that type's own fields or variants.

Two consequences follow. First, moving the attribute is a real repair only when it lands on the definition of a struct, an enum, or a union - moving it from a function to an impl block, or from a trait to a type alias, moves the error with it, because none of those items is a data structure either. Second, when the item carrying the attribute is the item that actually needs the trait - a type whose Debug output must be written by hand, for example - the repair is not a move at all: delete the attribute and write the implementation yourself.

The Rust Book makes the same point from the trait side: the derive attribute "can apply to a struct or enum definition" and "generates code that will implement a trait with its own default implementation on the type you've annotated". It also marks the boundary of what can be derived at all: the traits in its appendix are the only standard library traits with derive support, a trait such as Display "can't be derived" and must be implemented by hand, and libraries can add derives for their own traits through procedural macros. None of that changes the placement rule - a library's custom derive is still an attribute that belongs on a data type, and on any other item it draws this same error.

How to read the diagnostic

The header is "derive may only be applied to structs, enums and unions", with the code E0774, and the diagnostic carries two labels and no help line. The first label spans the attribute itself, #[derive(Debug)], and reads "not applicable here". The second label spans the item under it - one line for a function signature or a constant, the whole block for an impl - and reads "not a struct, enum or union". There is no suggested rewrite to accept: the compiler does not guess whether the attribute was meant for a nearby type or was pasted in by mistake, so the choice between deleting it and moving it is yours.

Each misplaced attribute is counted separately. Two derive attributes stacked on one function draw the error twice, once per attribute, and the summary line reports "2 previous errors". A single attribute naming several traits, #[derive(Debug, Clone)], on a wrong item is still one attribute and draws the error once.

The position inside a definition matters as much as the kind of item. An attribute on an associated type inside a trait is E0774, as the error index's own example shows. An attribute on an enum variant or a struct field is a different failure with no error code at all - the traps section works both.

The first failure: derive on a function

This is the most common shape in real code: an attribute copied from a type definition onto the function below it, or left behind when a struct was replaced by a function. The attribute sits on a free function:

Rust
#[derive(Debug)]
fn four() -> i32 {
    4
}
fn main() {
    println!("{}", four());
}
Output
error[E0774]: `derive` may only be applied to `struct`s, `enum`s and `union`s
 --> a1.rs:1:1
  |
1 |   #[derive(Debug)]
  |   ^^^^^^^^^^^^^^^^ not applicable here
2 | / fn four() -> i32 {
3 | |     4
4 | | }
  | |_- not a `struct`, `enum` or `union`

error: aborting due to 1 previous error

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

The function itself is fine - it returns 4 and main prints it. What the compiler rejects is the attribute above it: a function is not a data structure, so there is no type for a generated Debug implementation to belong to. The repair deletes the attribute:

Rust
fn four() -> i32 {
    4
}
fn main() {
    println!("{}", four());
}
Output
4

The output is 4. If the point of the attribute was to print a value for debugging, that belongs to the value's type, not to the function that produces it - the type's definition is where the attribute goes.

The second failure: derive on a trait

A trait definition is also not a data type. Deriving a trait for a trait makes the same category mistake in the other direction:

Rust
#[derive(Debug)]
trait Greeter {
    fn value(&self) -> i32;
}
struct Unit;
impl Greeter for Unit {
    fn value(&self) -> i32 {
        4
    }
}
fn main() {
    println!("{}", Unit.value());
}
Output
error[E0774]: `derive` may only be applied to `struct`s, `enum`s and `union`s
 --> a2.rs:1:1
  |
1 |   #[derive(Debug)]
  |   ^^^^^^^^^^^^^^^^ not applicable here
2 | / trait Greeter {
3 | |     fn value(&self) -> i32;
4 | | }
  | |_- not a `struct`, `enum` or `union`

error: aborting due to 1 previous error

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

The span covers the trait's first line and the label says what the trait is not. A trait can require other traits as supertraits, and a type can derive an implementation of this trait only if a derive macro for it exists - but the trait definition itself carries no fields, so no implementation can be generated for it. Deleting the attribute leaves the trait and its implementation untouched:

Rust
trait Greeter {
    fn value(&self) -> i32;
}
struct Unit;
impl Greeter for Unit {
    fn value(&self) -> i32 {
        4
    }
}
fn main() {
    println!("{}", Unit.value());
}
Output
4

The output is 4, the value the implementing type returns. Nothing about the trait changed; the attribute was the whole defect.

The third failure: derive on an impl block - and the move that repairs it

An impl block looks like the place an implementation is generated, which is exactly why the attribute lands here. It is still the wrong item:

Rust
struct Point {
    x: i32,
}
#[derive(Debug)]
impl Point {}
fn main() {
    let p = Point { x: 4 };
    println!("{}", p.x);
}
Output
error[E0774]: `derive` may only be applied to `struct`s, `enum`s and `union`s
 --> a3.rs:4:1
  |
4 | #[derive(Debug)]
  | ^^^^^^^^^^^^^^^^ not applicable here
5 | impl Point {}
  | ------------- not a `struct`, `enum` or `union`

error: aborting due to 1 previous error

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

The second label spans the whole impl block. This is the one position where the usual intent is clear: the attribute was meant for the type the impl block is about. The repair moves it onto that type's definition - here the struct above the impl block - and drops the now-empty impl block:

Rust
#[derive(Debug)]
struct Point {
    x: i32,
}
fn main() {
    let p = Point { x: 4 };
    println!("{p:?} {}", p.x);
}
Output
Point { x: 4 } 4

The output is Point { x: 4 } 4: the derived Debug output for the struct, then the field read directly. The move works because the attribute now sits on a struct. The same attribute moved to any other non-type item - the function that constructs the struct, say - would draw the same error again.

The same error on an alias, a constant, a static, a module, and a use declaration

The remaining item kinds all draw the identical diagnostic with the same two labels, and for each of them the repair is deletion - none of them is a type the attribute could be moved within the item itself. A type alias names a type but is not a type definition; the alias for i32 has no fields of its own to derive from:

Rust
#[derive(Debug)]
type Count = i32;
fn main() {
    let n: Count = 4;
    println!("{n}");
}
Output
error[E0774]: `derive` may only be applied to `struct`s, `enum`s and `union`s
 --> a4.rs:1:1
  |
1 | #[derive(Debug)]
  | ^^^^^^^^^^^^^^^^ not applicable here
2 | type Count = i32;
  | ----------------- not a `struct`, `enum` or `union`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0774`.
Rust
type Count = i32;
fn main() {
    let n: Count = 4;
    println!("{n}");
}
Output
4

The output is 4. If values of the alias need a derived trait, the derive belongs on a struct that wraps the value, not on the alias. A constant:

Rust
#[derive(Debug)]
const LIMIT: i32 = 4;
fn main() {
    println!("{LIMIT}");
}
Output
error[E0774]: `derive` may only be applied to `struct`s, `enum`s and `union`s
 --> a5.rs:1:1
  |
1 | #[derive(Debug)]
  | ^^^^^^^^^^^^^^^^ not applicable here
2 | const LIMIT: i32 = 4;
  | --------------------- not a `struct`, `enum` or `union`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0774`.
Rust
const LIMIT: i32 = 4;
fn main() {
    println!("{LIMIT}");
}
Output
4

The output is 4. A static draws the same error in the same form - a static is a value with a fixed address, not a type:

Rust
#[derive(Debug)]
static VALUE: i32 = 4;
fn main() {
    println!("{VALUE}");
}
Output
error[E0774]: `derive` may only be applied to `struct`s, `enum`s and `union`s
 --> a6.rs:1:1
  |
1 | #[derive(Debug)]
  | ^^^^^^^^^^^^^^^^ not applicable here
2 | static VALUE: i32 = 4;
  | ---------------------- not a `struct`, `enum` or `union`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0774`.
Rust
static VALUE: i32 = 4;
fn main() {
    println!("{VALUE}");
}
Output
4

The output is 4. A module is a namespace, not a type:

Rust
#[derive(Debug)]
mod helpers {
    pub fn four() -> i32 {
        4
    }
}
fn main() {
    println!("{}", helpers::four());
}
Output
error[E0774]: `derive` may only be applied to `struct`s, `enum`s and `union`s
 --> a7.rs:1:1
  |
1 |   #[derive(Debug)]
  |   ^^^^^^^^^^^^^^^^ not applicable here
2 | / mod helpers {
3 | |     pub fn four() -> i32 {
4 | |         4
5 | |     }
6 | | }
  | |_- not a `struct`, `enum` or `union`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0774`.
Rust
mod helpers {
    pub fn four() -> i32 {
        4
    }
}
fn main() {
    println!("{}", helpers::four());
}
Output
4

The output is 4, returned by the function inside the module - and note that the function inside was never the problem; the attribute on the module around it was. A use declaration imports a name and defines nothing:

Rust
#[derive(Debug)]
use std::fmt::Debug;
fn show(x: &dyn Debug) {
    println!("{x:?}");
}
fn main() {
    show(&4);
}
Output
error[E0774]: `derive` may only be applied to `struct`s, `enum`s and `union`s
 --> a8.rs:1:1
  |
1 | #[derive(Debug)]
  | ^^^^^^^^^^^^^^^^ not applicable here
2 | use std::fmt::Debug;
  | -------------------- not a `struct`, `enum` or `union`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0774`.
Rust
use std::fmt::Debug;
fn show(x: &dyn Debug) {
    println!("{x:?}");
}
fn main() {
    show(&4);
}
Output
4

The output is 4, printed through the imported Debug trait. The import itself was doing real work in this program - it names the trait the helper function bounds on - and deleting only the attribute keeps it.

The error index's own shape: derive on an associated type

The one position inside another item that still draws E0774 is an associated type. This is the error codes index's erroneous example, completed into a program:

Rust
trait Foo {
    #[derive(Clone)]
    type Bar;
}
struct S;
impl Foo for S {
    type Bar = i32;
}
fn main() {
    let _: <S as Foo>::Bar = 4;
}
Output
error[E0774]: `derive` may only be applied to `struct`s, `enum`s and `union`s
 --> a9.rs:2:5
  |
2 |     #[derive(Clone)]
  |     ^^^^^^^^^^^^^^^^ not applicable here
3 |     type Bar;
  |     --------- not a `struct`, `enum` or `union`

error: aborting due to 1 previous error

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

The attribute sits inside the trait, on its associated type declaration, and the labels point there: the attribute span reads "not applicable here", and type Bar; is labeled "not a struct, enum or union". An associated type is a placeholder each implementation fills in; it has no definition of its own at the declaration, so there is nothing to generate an implementation from. The repair deletes the attribute and leaves each implementation to name its concrete type:

Rust
trait Foo {
    type Bar;
    fn bar(&self) -> Self::Bar;
}
struct S;
impl Foo for S {
    type Bar = i32;
    fn bar(&self) -> i32 {
        4
    }
}
fn main() {
    println!("{}", S.bar());
}
Output
4

The output is 4, the value the implementation's bar returns, with Bar resolved to i32 by the implementation itself. If every implementation's Bar needs Clone, that requirement is stated as a bound on the associated type, type Bar: Clone; - a bound is how a trait asks for a trait, and it is not spelled with a derive attribute.

The traps

Trap
Three shapes sit next to this error and behave differently.

A derive on a trait implementation is E0774, not a way to generate that implementation. This program tries to derive the very Debug implementation it writes by hand:

Rust
struct Unit;
#[derive(Debug)]
impl std::fmt::Debug for Unit {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Unit")
    }
}
fn main() {
    println!("{:?}", Unit);
}
Output
error[E0774]: `derive` may only be applied to `struct`s, `enum`s and `union`s
 --> n8.rs:2:1
  |
2 |   #[derive(Debug)]
  |   ^^^^^^^^^^^^^^^^ not applicable here
3 | / impl std::fmt::Debug for Unit {
4 | |     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5 | |         write!(f, "Unit")
6 | |     }
7 | | }
  | |_- not a `struct`, `enum` or `union`

error: aborting due to 1 previous error

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

The second label spans the whole impl block, as with the inherent impl block above. A hand-written implementation and a derived one are alternatives: the repair keeps the hand-written body and deletes the attribute:

Rust
struct Unit;
impl std::fmt::Debug for Unit {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Unit")
    }
}
fn main() {
    println!("{:?}", Unit);
}
Output
Unit

The output is Unit, the text the hand-written fmt produces. Keeping both was never an option - with the attribute deleted, the manual implementation is the implementation.

Stacked attributes multiply the error. The Reference notes the derive attribute may be used any number of times on an item - on a struct, two attributes work like one longer list. On a function, each attribute is judged on its own and each one fails:

Rust
#[derive(Debug)]
#[derive(Clone)]
fn four() -> i32 {
    4
}
fn main() {
    println!("{}", four());
}
Output
error[E0774]: `derive` may only be applied to `struct`s, `enum`s and `union`s
 --> n4.rs:1:1
  |
1 |   #[derive(Debug)]
  |   ^^^^^^^^^^^^^^^^ not applicable here
2 |   #[derive(Clone)]
3 | / fn four() -> i32 {
4 | |     4
5 | | }
  | |_- not a `struct`, `enum` or `union`

error[E0774]: `derive` may only be applied to `struct`s, `enum`s and `union`s
 --> n4.rs:2:1
  |
2 |   #[derive(Clone)]
  |   ^^^^^^^^^^^^^^^^ not applicable here
3 | / fn four() -> i32 {
4 | |     4
5 | | }
  | |_- not a `struct`, `enum` or `union`

error: aborting due to 2 previous errors

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

Both diagnostics name the same function, the summary counts "2 previous errors", and deleting only one attribute leaves the other error standing. On a wrong item, every derive attribute must go.

A derive on a part of a type, rather than on the type, is not E0774 at all. An enum variant cannot carry the attribute:

Rust
enum Shape {
    #[derive(Debug)]
    Square,
    Circle,
}
fn main() {
    let _ = Shape::Square;
}
Output
error: expected non-macro attribute, found attribute macro `derive`
 --> n5.rs:2:7
  |
2 |     #[derive(Debug)]
  |       ^^^^^^ not a non-macro attribute

error: aborting due to 1 previous error

and neither can a struct field:

Rust
struct Point {
    #[derive(Debug)]
    x: i32,
}
fn main() {
    let _ = Point { x: 4 };
}
Output
error: expected non-macro attribute, found attribute macro `derive`
 --> n6.rs:2:7
  |
2 |     #[derive(Debug)]
  |       ^^^^^^ not a non-macro attribute

error: aborting due to 1 previous error

Both fail with the un-coded error "expected non-macro attribute, found attribute macro derive" - in those positions the attribute is rejected before the placement rule is even reached, because a variant and a field are not items an attribute macro can attach to. The repair is the same move in both cases, onto the enclosing enum or struct definition; the error to search for is not.

A derive naming a macro that does not exist is also a different error, even on the right item. This attribute sits on a struct, where placement is legal:

Rust
#[derive(Nope)]
struct Point {
    x: i32,
}
fn main() {
    let _ = Point { x: 4 };
}
Output
error: cannot find derive macro `Nope` in this scope
 --> n7.rs:1:10
  |
1 | #[derive(Nope)]
  |          ^^^^

error: aborting due to 1 previous error

The failure is "cannot find derive macro Nope in this scope", with no error code: placement was never the problem, the name was. Built-in derives need no import; a library's derive must be in scope, typically through the library's own import. When this error appears, fix the name or the import - moving the attribute changes nothing.

For contrast, the legal positions are exactly three. A struct was worked above; an enum derives the same way:

Rust
#[derive(Debug)]
enum Shape {
    Square,
    Circle,
}
fn main() {
    println!("{:?} {:?}", Shape::Square, Shape::Circle);
}
Output
Square Circle

The output is Square Circle, both variants printed through the one derived Debug implementation. And a union takes the attribute too, for the derives a union supports:

Rust
#[derive(Copy, Clone)]
union Word {
    x: i32,
}
fn main() {
    let u = Word { x: 4 };
    let v = u;
    println!("{}", unsafe { v.x });
}
Output
4

The output is 4, copied out of the union through the derived Copy. Struct, enum, union - if the item carrying the attribute is none of those three words, the attribute is the defect.

Fixes in reach order

When E0774 appears, work the item in this order. First, read the second label: it names the item the attribute sits on. If that item is a struct, an enum, or a union, the error is not placement - check the derive name instead. Second, if the attribute was meant for a type defined nearby - the common impl-block case - move it onto that type's definition and delete any block it leaves empty. Third, if the item itself needs the trait, delete the attribute and write the implementation by hand, as the trait-implementation trap shows. Fourth, in every other case - a function, a trait, an alias, a constant, a static, a module, a use declaration, an associated type - delete the attribute; for an associated type, state any real requirement as a bound. Last, count the attributes: stacked derive attributes on one wrong item are one error each, and all of them must be repaired before the program compiles.

Decision summary

A derive attribute generates a trait implementation for one data type, so it is legal on exactly three kinds of item: a struct, an enum, and a union. On any other item - a function, a trait, an impl block, a type alias, a constant, a static, a module, a use declaration, or an associated type inside a trait - it is E0774, "derive may only be applied to structs, enums and unions", with two labels and no suggested rewrite. The repair is chosen by intent: move the attribute onto the type it was meant for when that type is defined nearby, write the implementation by hand when the item itself needs the trait, and delete the attribute in every other case. Two nearby shapes are not this error: a derive on an enum variant or a struct field fails with the un-coded "expected non-macro attribute" error, and a derive naming an unknown macro fails with "cannot find derive macro", even on a struct.

Checklist

  • Every derive attribute sits on a struct, an enum, or a union definition - never on a function, trait, impl block, alias, constant, static, module, or use declaration.
  • An attribute moved off an impl block lands on the type's definition, and any block it leaves empty is removed.
  • A trait an item needs without a data type behind it is implemented by hand, with no derive attribute on the impl.
  • An associated type that needs a trait states it as a bound, not as a derive attribute.
  • Every stacked derive attribute on a wrong item is removed - each one is a separate E0774.
  • A variant or field carrying the attribute is moved to the enclosing type, and an unknown derive name is fixed by name or import, not by placement.

Sources

  • Rust error codes index, E0774 - derive was applied on something which is not a struct, a union or an enum, its erroneous #[derive(Clone)] on an associated type inside a trait, and its working example with the attribute on a struct.
  • The Rust Reference, Derive - the derive attribute generates items for data structures, may only be applied to structs, enums, and unions, may be used any number of times on an item, and the list of built-in derives.
  • The Rust Programming Language, Appendix C: Derivable Traits - derive applies to a struct or enum definition and generates a trait implementation with default behavior, the standard traits that can be derived, and that traits such as Display cannot be derived and must be implemented manually.

Keep Exploring