Rust

E0605 Invalid Cast Fix

The `as` operator converts between primitive types - it does not take a value apart, build a collection, or invent a place in memory. E0605 is the compiler saying a cast crossed that line: an array, a struct, a vector, a reference was asked for where no primitive-to-primitive conversion exists. This guide separates the five shapes that produce it and matches each one to its repair: cast the piece you actually mean, construct the collection, or borrow a real value.

Written November 24, 2026 · Tested with Rust 1.99.1 · 23 min read

By the end of this guide
  • The rule underneath: as converts only between the pairs in the cast table
  • How to read the diagnostic
  • The first failure: casting an array to an integer
  • The second failure: casting a struct value to an integer

Written November 24, 2026

Tested with Rust 1.99.1

What this guide is

Error E0605 is a cast between types that are not both primitives. The error codes index defines it in one sentence - an invalid cast was attempted - and its two erroneous examples are the shapes this guide is built from: an integer cast to a Vec, and a raw pointer cast to a reference. The header names both types - "non-primitive cast: [i32; 2] as u8" - and that pair is the whole diagnosis: the type on the left of as, the type on the right, and a conversion between them that the cast table does not contain.

The forms in this guide each reproduce E0605 with a small, complete program, and each asks as to do a different kind of work it cannot do. An array cast to an integer. A struct value cast to an integer. An integer cast to a vector. A raw pointer cast to a reference. An integer cast to a reference. The fixes fall into three shapes: cast the piece you actually mean - an element, a field - instead of the container; construct the target value with its own syntax, as vec![] constructs a vector; or form the reference by borrowing or dereferencing a real value, because a reference needs a place in memory and a cast cannot invent one.

Several neighboring results are not the plain E0605 shape and are worked only in the traps and the checklist. A bool cast to an integer compiles, and so does a raw pointer cast to usize - both pairs are in the cast table. The unit type cast to an integer is E0605 itself. Casting a string slice to an integer is not E0605: it is E0606. Casting a float to char is not E0605 either: it is E0604. And casting an integer to bool is E0054, which fails before the cast table is even consulted in the same way.

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: as converts only between the pairs in the cast table

The Rust Reference's type cast expressions section states the rule directly: as can be used to explicitly perform coercions, as well as the additional casts in its table, and any cast that does not fit either a coercion rule or an entry in the table is a compiler error. The table's rows are the whole menu. An integer or float to an integer or float is a numeric cast. An enumeration to an integer is an enum cast. A bool or char to an integer is a primitive-to-integer cast. A u8 to a char is the one cast into char. A raw pointer to a raw pointer, a raw pointer to an integer, and an integer to a raw pointer cover addresses. A function item to a function pointer, and a closure to a function pointer, cover callable values. Nothing in the table takes a compound value - an array, a tuple, a struct - apart, nothing builds a collection, and nothing produces a reference.

The error codes index states the same boundary from the other side: only primitive types can be cast into each other. Its passing examples stay inside the table - a u8 cast to a u32, a *const u8 cast to a *const i8 - and its failing examples step outside it in the two directions the families below generalize: a primitive cast to a non-primitive collection type, and a raw pointer cast to a reference type.

That boundary explains why the fixes never look like a better cast. When the wanted value is inside the compound value, the program names the piece first - an index, a field - and casts the piece, which is a primitive. When the wanted value is a collection, the program constructs it. When the wanted value is a reference, the program borrows a value that exists, or dereferences a pointer to one, because a reference is not a bit pattern a cast can reinterpret - it is a borrow of a place, with a lifetime attached.

How to read the diagnostic

Every failure in the five families prints the same shape of output. The first family's, in full:

Terminal
$ rustc --edition 2021 fail_array.rs -o fail_array
error[E0605]: non-primitive cast: `[i32; 2]` as `u8`
 --> fail_array.rs:3:13
  |
3 |     let n = a as u8;
  |             ^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object

error: aborting due to 1 previous error

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

Read it in order. The header names the pair - the source type first, the target type second - and calls the cast non-primitive: at least one side of the pair is outside the cast table. The caret span underlines the whole cast expression, and its note restates the rule: an as expression can only be used to convert between primitive types or to coerce to a specific trait object. Two of the families below print a shorter note instead - "invalid cast", with a help suggestion - because the pair is closer to a legal one: a raw pointer and a reference point at the same kind of thing, and an integer and a reference to an integer differ by one borrow. The help in those cases names the repair directly: borrow the value, or dereference the pointer.

That pair in the header is also the fastest way to tell this error from its neighbors. When one side is a reference to a sized value being cast to a smaller primitive - a string slice or a reference to an integer cast to u8 - the header is a single-type "casting ... is invalid" sentence under E0606, in the traps. When the target is char and the source is a primitive other than u8, the header names that restriction under E0604; a non-primitive source cast to char is E0605, and a reference source cast to char is E0606. And when the target is bool and the source is a primitive, the header is E0054's "cannot cast" - bool is a cast source in the table, never a cast target; a non-primitive source cast to bool is E0605 instead, and a reference source cast to bool is E0606.

The first failure: casting an array to an integer

A program that casts a whole array where a single element was meant:

Rust
fn main() {
    let a = [7i32, 8];
    let n = a as u8;
    println!("{}", n);
}
Terminal
$ rustc --edition 2021 fail_array.rs -o fail_array
error[E0605]: non-primitive cast: `[i32; 2]` as `u8`
 --> fail_array.rs:3:13
  |
3 |     let n = a as u8;
  |             ^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object

error: aborting due to 1 previous error

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

The array [7, 8] has type [i32; 2] - a compound value holding two integers, not an integer itself. There is no row in the cast table with an array on either side, so the cast fails before any question of which element, or what truncation, could arise. The shape appears wherever an aggregate is handled as if it were its first member: a fixed-size buffer, a coordinate pair stored as an array, a two-element result passed along whole.

The fix indexes the element first, then casts that element:

Rust
fn main() {
    let a = [7i32, 8];
    let n = a[0] as u8;
    println!("{}", n);
}
Terminal
$ rustc --edition 2021 fix_array.rs -o fix_array
$ ./fix_array
7

The output is 7. Indexing produces an i32 - a primitive - and i32 to u8 is an ordinary numeric cast, in the table. The cast did not need to become cleverer; it needed a smaller input. If the wanted number had been a combination of the elements - a sum, a packed pair of bytes - the fix is the same shape: compute the primitive value with ordinary expressions, then cast the result if a cast is still needed at all.

The second failure: casting a struct value to an integer

A program that casts a struct value where its field was meant:

Rust
struct Pair {
    value: i32,
}

fn main() {
    let p = Pair { value: 7 };
    let n = p as u8;
    println!("{}", n);
}
Terminal
$ rustc --edition 2021 fail_struct.rs -o fail_struct
error[E0605]: non-primitive cast: `Pair` as `u8`
 --> fail_struct.rs:7:13
  |
7 |     let n = p as u8;
  |             ^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object

error: aborting due to 1 previous error

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

Pair is a user-defined type. It is not a primitive, it has no entry in the cast table, and the compiler does not guess that the single field inside it is the intended value - a struct with two fields, or with a field of a different type, would make any such guess arbitrary. The shape appears when a wrapper type - a measurement, an identifier, a validated number - is used at a boundary that wants the plain number, and the unwrapping step is written as a cast.

The fix reads the field first, then casts that field:

Rust
struct Pair {
    value: i32,
}

fn main() {
    let p = Pair { value: 7 };
    let n = p.value as u8;
    println!("{}", n);
}
Terminal
$ rustc --edition 2021 fix_struct.rs -o fix_struct
$ ./fix_struct
7

The output is 7. The field access produces the i32 the program actually meant, and the cast from there is the ordinary numeric one. A tuple value fails the same way for the same reason, and is repaired the same way: name the component - .0, .1 - and cast the component. Where the conversion is a standing part of a type's meaning rather than a one-off at a call site, the same idea takes its longer form: a method on the type that returns the field, or a From implementation, so callers convert through the type's own API instead of reaching for as.

The third failure: casting an integer to a Vec

A program that casts a single byte where a one-element vector was meant - the error index's own first example, in runnable form:

Rust
fn main() {
    let x = 7u8;
    let v = x as Vec<u8>;
    println!("{:?}", v);
}
Terminal
$ rustc --edition 2021 fail_vec.rs -o fail_vec
error[E0605]: non-primitive cast: `u8` as `Vec<u8>`
 --> fail_vec.rs:3:13
  |
3 |     let v = x as Vec<u8>;
  |             ^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object

error: aborting due to 1 previous error

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

Here the source side is a primitive and the target side is not: Vec<u8> is a heap-allocated collection type, and no cast constructs one. A cast reinterprets or converts a value that already exists; it does not allocate, does not choose a length, and does not build a container around the value. The shape appears in code ported from languages where a scalar can be promoted to a one-element collection implicitly, and in quick drafts where as is reached for as a general-purpose "make it this type" operator.

The fix constructs the vector with the vec! macro:

Rust
fn main() {
    let x = 7u8;
    let v = vec![x];
    println!("{:?}", v);
}
Terminal
$ rustc --edition 2021 fix_vec.rs -o fix_vec
$ ./fix_vec
[7]

The output is [7]. The macro is the construction syntax for a vector literal: the element goes inside it, and the result is a real Vec<u8> with length one. The same repair covers the other collection targets - an array literal [x] where an array was wanted, String::from or to_string where a string was wanted. In each case the target type has its own construction forms, and those forms, not as, are how a value of that type comes to exist.

The fourth failure: casting a raw pointer to a reference

A program that casts a raw pointer where a reference was meant - the error index's second example:

Rust
fn main() {
    let v = core::ptr::null::<u8>();
    let r = v as &u8;
    println!("{}", r);
}
Terminal
$ rustc --edition 2021 fail_ptr_ref.rs -o fail_ptr_ref
error[E0605]: non-primitive cast: `*const u8` as `&u8`
 --> fail_ptr_ref.rs:3:13
  |
3 |     let r = v as &u8;
  |             ^^^^^^^^ invalid cast
  |
help: consider borrowing the value
  |
3 -     let r = v as &u8;
3 +     let r = &*v;
  |

error: aborting due to 1 previous error

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

A raw pointer and a reference are not two spellings of one thing. A *const u8 is an address with no promises attached - it may be null, as it is here, it may dangle, and nothing tracks how long it stays valid. A &u8 is a borrow: it promises a live, valid value for the whole of its lifetime. A cast cannot manufacture that promise, so the pair is outside the table even though a pointer-to-pointer cast between raw pointers, and a pointer-to-integer cast, are both inside it. The diagnostic's help names the real operation - &*v, dereference and borrow - and the fix below follows it, on a pointer that actually points at a value.

The fix points the pointer at a live value and forms the reference by dereferencing it inside unsafe:

Rust
fn main() {
    let x = 7u8;
    let v: *const u8 = &x;
    let r = unsafe { &*v };
    println!("{}", r);
}
Terminal
$ rustc --edition 2021 fix_ptr_ref.rs -o fix_ptr_ref
$ ./fix_ptr_ref
7

The output is 7. The unsafe block is the point: forming a reference from a raw pointer is an operation a programmer vouches for, not a conversion the compiler performs silently, because the compiler cannot check that the pointer is non-null, aligned, and pointing at a live value - here it can be trusted because the pointer was just taken from x, which outlives the reference. Dereferencing the null pointer from the failing program this way would be undefined behavior, not a repair; where the pointer comes from outside the program's control, it is checked first, and where no pointer was needed at all, the reference is taken from the value directly, as the next family does.

The fifth failure: casting an integer to a reference

A program that casts an integer literal where a borrowed value was meant:

Rust
fn main() {
    let r = 5 as &i32;
    println!("{}", r);
}
Terminal
$ rustc --edition 2021 fail_int_ref.rs -o fail_int_ref
error[E0605]: non-primitive cast: `i32` as `&i32`
 --> fail_int_ref.rs:2:13
  |
2 |     let r = 5 as &i32;
  |             ^^^^^^^^^ invalid cast
  |
help: consider borrowing the value
  |
2 |     let r = &5 as &i32;
  |             +

error: aborting due to 1 previous error

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

A reference is the opposite direction from the previous family and fails for the mirror reason. 5 is a value, not a place: it has no address of its own for a &i32 to point at, and a cast cannot allocate a home for it and lend that home a lifetime. The cast table contains integer-to-raw-pointer - an integer reinterpreted as an address - but no integer-to-reference row, because a raw pointer carries no validity promise and a reference does. The shape appears in test code and call sites that want to pass a literal where a &i32 parameter is expected, written by analogy with passing a literal where an i32 is expected.

The fix gives the value a name, then borrows it:

Rust
fn main() {
    let x = 5i32;
    let r = &x;
    println!("{}", r);
}
Terminal
$ rustc --edition 2021 fix_int_ref.rs -o fix_int_ref
$ ./fix_int_ref
5

The output is 5. The binding x is the place the reference points at, and &x is the borrow of it - the reference lives exactly as long as x does, which is the fact the type system needed and the cast could not supply. The diagnostic's own help suggests the shorter form for a literal, &5, where the compiler creates a temporary with a promoted lifetime; the named binding is the form that stays clear about how long the borrowed value lives, and the one to use when the reference is stored or returned from anywhere but the immediate expression.

The fixes in reach order

  • Read the header's pair: the source type, then the target type. At least one side is outside the cast table, and which side it is chooses the repair.
  • The source is a compound value and the target is a primitive: name the piece first - an index for an array, a field for a struct, a component for a tuple - then cast the piece.
  • The target is a collection or another constructed type: build it with that type's own syntax - vec![], an array literal, a constructor - instead of casting to it.
  • The target is a reference: borrow a value that has a place and a lifetime, or dereference a raw pointer to a live value inside unsafe after the pointer's validity is established.
  • The source is a raw pointer and the target is a reference: the same rule from the other side - no cast forms a borrow; &*p inside unsafe, on a pointer known to be valid, is the operation.
  • The pair is a string slice or a reference cast to a primitive, a float cast to char, or a primitive cast to bool: the header's code is E0606, E0604, or E0054 respectively, each repaired by its own rule - parse or dereference, convert through u8 or char::from_u32, compare against zero.
  • Both sides are primitives and the cast still looks wrong: it is in the table and it compiles - the remaining questions are what it does to the value, truncation and saturation, which are semantics, not E0605.

Traps

  • A bool cast to an integer compiles - bool to an integer type is a row in the cast table, a primitive-to-integer cast. Here true becomes 1:
Rust
fn main() {
    let n = true as u8;
    println!("{}", n);
}
Terminal
$ rustc --edition 2021 trap_bool.rs -o trap_bool
$ ./trap_bool
1
Trap
The output is 1. The Reference states the values directly: false casts to 0, true casts to 1. The reverse direction is the one that fails, and it fails under a different code - the integer-to-bool trap below. The pair is a useful calibration for the whole error: E0605 is about which pairs the table contains, not about whether the two types feel convertible - bool and u8 convert in exactly one direction. - A raw pointer cast to usize compiles - pointer-to-address is a row in the cast table. Here the address of a local is cast and tested for non-zero, which keeps the printed output stable:
Rust
fn main() {
    let x = 7i32;
    let p: *const i32 = &x;
    let n = p as usize;
    println!("{}", n != 0);
}
Terminal
$ rustc --edition 2021 trap_ptr_usize.rs -o trap_ptr_usize
$ ./trap_ptr_usize
true

The output is true. This is the legal neighbor the fourth family is most often confused with: the same raw pointer that cannot be cast to a reference can be cast to an integer, because the integer makes no promise about what lives at the address. The Reference notes the practical detail: casting a raw pointer to an integer produces the machine address, and using usize avoids truncating it, since usize is the pointer-sized integer type. - The unit type cast to an integer is E0605 itself. Here the only value of () is cast:

Rust
fn main() {
    let n = () as u8;
    println!("{}", n);
}
Terminal
$ rustc --edition 2021 trap_unit.rs -o trap_unit
error[E0605]: non-primitive cast: `()` as `u8`
 --> trap_unit.rs:2:13
  |
2 |     let n = () as u8;
  |             ^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object

error: aborting due to 1 previous error

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

The header's pair is () as u8. The unit type is a real type with one value, but it is not a primitive in the cast table's sense, and it carries no number to convert - a function that returns nothing returns (), and there is no count, code, or flag inside it to recover. Code that reaches this error usually expected the function or expression to produce a number and it produced () instead; the repair is at the expression - return or compute the number - not at the cast. Writing an explicit 0 where a default was genuinely meant also compiles, and says so honestly. - Casting a string slice to an integer fails - with E0606, not E0605. Here the text "42" is cast, in the shape most people try first when they meet this error family:

Rust
fn main() {
    let s = "42";
    let n = s as u8;
    println!("{}", n);
}
Terminal
$ rustc --edition 2021 trap_str.rs -o trap_str
error[E0606]: casting `&str` as `u8` is invalid
 --> trap_str.rs:3:13
  |
3 |     let n = s as u8;
  |             ^^^^^^^
  |
  = help: cast through a raw pointer first

error: aborting due to 1 previous error

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

The header is a single-type sentence - "casting &str as u8 is invalid" - and the help offers to cast through a raw pointer, which would produce an address, not the number the text spells. E0606 covers casts from a reference or slice type to a primitive or raw-pointer type that the table does not allow, where E0605 covers pairs involving a non-primitive type - a cast from a reference to another reference, such as &i32 as &u8, is E0605, not E0606. The repair for the actual intent is parsing: "42".parse::<u8>() returns a Result holding 42, because turning text into a number is a fallible conversion with its own API, not a cast. The same code and the same repair cover a reference to an integer cast to an integer - &i32 as u8 is E0606, repaired by dereferencing first, *r as u8. - Casting a float to char fails - with E0604, not E0605. Here a whole-number float is cast, on the theory that it holds a code point:

Rust
fn main() {
    let c = 65.0f64 as char;
    println!("{}", c);
}
Terminal
$ rustc --edition 2021 trap_float_char.rs -o trap_float_char
error[E0604]: only `u8` can be cast as `char`, not `f64`
 --> trap_float_char.rs:2:13
  |
2 |     let c = 65.0f64 as char;
  |             ^^^^^^^^^^^^^^^ invalid cast
  |
help: consider using `char::from_u32` instead (via a `u32`)
 --> trap_float_char.rs:2:13
  |
2 |     let c = 65.0f64 as char;
  |             ^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

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

The header states the restriction directly: only u8 can be cast as char. char is a Unicode code point, not a byte, and the one cast into it is deliberately narrow - 65u8 as char compiles and prints A. The diagnostic's help names the general route: char::from_u32, reached through a u32 - char::from_u32(65.0f64 as u32) returns Some('A'), as an Option, because not every u32 is a valid code point. A float therefore needs two honest steps, float to integer and integer to char with the validity check, where the single cast tried to skip both. - Casting an integer to bool fails - with E0054, not E0605. Here a u8 flag is cast:

Rust
fn main() {
    let b = 1u8 as bool;
    println!("{}", b);
}
Terminal
$ rustc --edition 2021 trap_int_bool.rs -o trap_int_bool
error[E0054]: cannot cast `u8` as `bool`
 --> trap_int_bool.rs:2:13
  |
2 |     let b = 1u8 as bool;
  |             ^^^^^^^^^^^
  |
help: compare with zero instead
  |
2 -     let b = 1u8 as bool;
2 +     let b = 1u8 != 0;
  |

error: aborting due to 1 previous error

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

The header is "cannot cast u8 as bool", and the help gives the repair inline: compare with zero instead, 1u8 != 0, which prints true. bool appears in the cast table only as a source - bool to integer, in the first trap - never as a target, because a cast into bool would have to pick a threshold and the language leaves that choice in the code, as an explicit comparison. Reading the code in the header keeps the family apart: E0605 means a non-primitive type was in the pair, E0054 means the target was bool, E0604 means the target was char from the wrong source, and E0606 means a reference or slice was cast where the table allows no such cast.

Checklist

  • The header's source-and-target pair read as the diagnosis - at least one side is outside the cast table, and which side chooses the repair.
  • A compound source - array, struct, tuple - taken apart by naming the piece, and the piece cast, never the container.
  • A collection target built with its own construction syntax, vec![] or a literal or a constructor, never reached with as.
  • A reference target formed by borrowing a named value, or by dereferencing a valid raw pointer inside unsafe - never by casting an integer or a raw pointer.
  • A raw pointer target kept distinct from a reference target: pointer-to-integer and pointer-to-pointer casts are in the table; pointer-to-reference is not.
  • Text that spells a number parsed with parse, and the failure read as E0606 when a string slice or reference was cast instead.
  • A char target reached only from u8 with as, or from a wider integer through char::from_u32 with its Option handled - a float-to-char failure read as E0604.
  • A bool target written as an explicit comparison against zero - an integer-to-bool failure read as E0054.
  • The fixed program run, and its output checked against what the program was meant to print.

Sources

  • Rust, Error codes index: E0605 — for the definition of the error as an invalid cast, its two erroneous examples (an integer cast to a Vec and a raw pointer cast to a reference), its statement that only primitive types can be cast into each other, and its passing examples of a u8 cast to a u32 and a *const u8 cast to a *const i8.
  • The Rust Reference, Operator expressions: type cast expressions — for as performing coercions plus the casts in its table and any other cast being a compiler error, for the table's rows (numeric casts, enum casts, bool or char to integer, u8 to char, pointer-to-pointer, pointer-to-address and address-to-pointer casts), for false casting to 0 and true to 1, for char casting to its code point value, and for a raw pointer cast to an integer producing the machine address with usize avoiding truncation.
  • The Rust Standard Library, Primitive Type str — for a string slice being a pointer and a length viewable with as_ptr and len, and for as_ptr converting a string slice to a raw pointer to its first byte, the pointer the parsing and borrowing repairs are chosen over.

Keep Exploring