Rust

E0606 Incompatible Cast Fix

An `as` cast is not a general conversion. It is a short, closed table of permitted pairs, and E0606 is what a pair outside the table draws. This guide works the shapes that fall outside it - references, strings, and slice references cast to numbers, scalars cast to pointers, and a trait-object pointer cast sideways - and matches each one to the conversion the types actually offer: a dereference, a parse, a length, or a fresh pointer built from the value.

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

By the end of this guide
  • The rule underneath: the cast table is closed
  • How to read the diagnostic
  • The first failure: a reference cast to an integer
  • The second failure: a string reference cast to an integer

Written November 29, 2026

Tested with Rust 1.99.1

What this guide is

Error E0606 is an incompatible cast: an as expression whose source and target pair is not one of the casts the language permits. The error codes index states the boundary in one sentence: "When casting, keep in mind that only primitive types can be cast into each other." Its example is a reference: x is a &u8, and x as u32 is invalid, while *x as u32 - dereference first, then cast the u8 - is the prescribed repair. A reference is not the number it points at, and no cast converts the one into the other.

The full rule is the cast table in the Rust Reference: "as can be used to explicitly perform coercions, as well as the following additional casts. 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 narrow. Numbers cast to numbers. A field-less enum, a bool, or a char casts to an integer; a u8 casts to a char. A raw pointer casts to a compatible raw pointer, to an integer address, and an integer casts back to a raw pointer. A function item casts to a function pointer. Anything not on a row - a reference to an integer, a scalar to a pointer, a pointer to a bool or a char - is refused, and which code refuses it depends on the shape of the miss, as the traps show.

The forms in this guide each reproduce E0606 with a small, complete program: a shared reference cast to an integer, a string reference cast to an integer, a slice reference cast to an integer, a bool cast to a raw pointer, a float cast to a raw pointer, and a trait-object pointer cast to a different trait's pointer. Each failure is followed by its repair. The repairs share one move - stop casting the thing in hand and convert the thing it stands for instead - but the concrete conversion differs every time: a dereference, a parse, a length, a code point, or a second pointer built from the value.

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: the cast table is closed

A cast never inspects, converts, or constructs. It reinterprets or resizes a value whose representation the table already covers, and that is all it can do. A &u8 holds an address, not the u8; turning it into a u32 holding the pointed-to value would require a load, which is a dereference - a different operation, written with a different symbol. A &str holds an address and a length; no single number is "the" number of a string until the program says whether it wants the parsed digits, the byte length, or the address. A bool has no address of its own to hand out as a pointer. In each case the cast is refused not because the conversion is impossible but because it is not the conversion a cast performs, and the repair is to write the operation that was actually meant.

That framing also explains the neighboring codes. The cast family has three refusal codes plus one older one, and the compiler picks by the shape of the miss. E0606 is the incompatible pair between types a cast could otherwise work on. E0605, "non-primitive cast", is drawn when a side of the cast is not a primitive type at all - a struct, a String, a tuple, a reference used as a target. E0604 is drawn by casts to char from anything other than u8. E0054 is drawn by casts to bool from the types it names. The failures below are the E0606 shapes; the traps work the neighbors, because the first repair instinct for an E0606 often lands on one of them.

How to read the diagnostic

The header names both types and the verdict: "casting &u8 as u32 is invalid". The caret span sits under the whole cast expression, not under either type. Sometimes a help block follows: for a reference cast to a number, the compiler proposes "dereference the expression" and spells the repaired line out, and that proposal is correct there. For the pointer-target shapes no help follows - there is no one-character repair for a bool cast to a pointer, because the program's intent is genuinely unclear. The trait-object shape carries a note instead: "the trait objects may have different vtables", which is the whole reason for that refusal in one line. Read the header first - it tells you the pair - then decide which value the program actually needs: the pointed-to value, a property of it, or its address. Each answer is a different repair.

The first failure: a reference cast to an integer

This is the error index's own shape. x is a &u8, and the cast asks for the u32 form of the reference itself:

Rust
fn main() { let x = &0u8; let y: u32 = x as u32; println!("{}", y); }
Output
error[E0606]: casting `&u8` as `u32` is invalid
 --> a1.rs:1:40
  |
1 | fn main() { let x = &0u8; let y: u32 = x as u32; println!("{}", y); }
  |                                        ^^^^^^^^
  |
help: dereference the expression
  |
1 | fn main() { let x = &0u8; let y: u32 = *x as u32; println!("{}", y); }
  |                                        +

error: aborting due to 1 previous error

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

The repair is the one the compiler proposes and the index prescribes: dereference first, so the cast runs on the u8, a pair the table covers:

Rust
fn main() { let x = &7u8; let y: u32 = *x as u32; println!("{}", y); }
Output
7

The output is 7, the value behind the reference. A mutable reference fails identically - a &mut u8 cast to u32 draws the same E0606 - and takes the same dereference repair. What a reference cast to an integer never yields is the address: addresses come from raw pointers, worked in the traps.

The second failure: a string reference cast to an integer

A &str is a reference, so it fails for the first failure's reason - and then keeps failing after a dereference, as the traps show, because the str behind it is not a number either:

Rust
fn main() { let s = "42"; let y: u32 = s as u32; println!("{}", y); }
Output
error[E0606]: casting `&str` as `u32` is invalid
 --> a2.rs:1:40
  |
1 | fn main() { let s = "42"; let y: u32 = s as u32; println!("{}", y); }
  |                                        ^^^^^^^^
  |
  = help: cast through a raw pointer first

error: aborting due to 1 previous error

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

There is no cast repair, because there are two different numbers a string might mean and the program must pick one. If the string holds digits, the conversion is a parse:

Rust
fn main() { let s = "42"; let y: u32 = s.parse().unwrap(); println!("{}", y); }
Output
42

The output is 42. If the number wanted is the string's size, the conversion is a length, and only the length - a plain integer - is cast:

Rust
fn main() { let s = "hello"; let y = s.len() as u32; println!("{}", y); }
Output
5

The output is 5, the byte length of "hello". A cast converts representations; turning text into a number is a computation, and computations are methods and functions, not as.

The third failure: a slice reference cast to an integer

A slice reference is a fat pointer - an address and a length - and casting it to a u32 asks one number to stand for both halves:

Rust
fn main() { let a = &[1, 2, 3]; let y: u32 = a as u32; println!("{}", y); }
Output
error[E0606]: casting `&[i32; 3]` as `u32` is invalid
 --> a4.rs:1:46
  |
1 | fn main() { let a = &[1, 2, 3]; let y: u32 = a as u32; println!("{}", y); }
  |                                              ^^^^^^^^
  |
  = help: cast through a raw pointer first

error: aborting due to 1 previous error

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

As with the string, the repair names the half the program wants. For the count, it is the length again:

Rust
fn main() { let a = &[1, 2, 3]; let y = a.len() as u32; println!("{}", y); }
Output
3

The output is 3. For the address half, the route is through a raw pointer - a slice reference coerces to a *const pointer to its first element, and a raw pointer casts to usize under the table's pointer-to-address row. Neither half is reachable by casting the reference itself.

The fourth failure: a bool cast to a raw pointer

The table's pointer rows run in specific directions: a raw pointer casts to an integer, and an integer casts to a raw pointer. A bool is on neither row as a pointer source:

Rust
fn main() { let b = true; let p = b as *const u8; println!("{:p}", p); }
Output
error[E0606]: casting `bool` as `*const u8` is invalid
 --> a5.rs:1:35
  |
1 | fn main() { let b = true; let p = b as *const u8; println!("{:p}", p); }
  |                                   ^^^^^^^^^^^^^^

error: aborting due to 1 previous error

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

No help follows, because there is nothing the cast could have meant: a bool is a value, not a location, and it has no address to reinterpret. What the program can do depends on which of two intents it had. If it wanted the numeric form of the flag, bool casts to an integer under its own row:

Rust
fn main() { let b = true; let n = b as u8; println!("{}", n); }
Output
1

The output is 1. If it genuinely wanted a pointer, the pointer must be taken from a value that lives somewhere - cast the flag to a byte, then take that byte's address:

Rust
fn main() { let b = true; let byte = b as u8; let p = &byte as *const u8; println!("{}", unsafe { *p }); }
Output
1

The output is 1, read back through the pointer: the byte holding the flag's numeric form. The pointer names the byte, never the bool cast directly.

The fifth failure: a float cast to a raw pointer

A float draws the same refusal for the same reason. Numbers cast to numbers; a float is not an integer, so it is not a pointer source either:

Rust
fn main() { let f = 1.5f64; let p = f as *const u8; println!("{:p}", p); }
Output
error[E0606]: casting `f64` as `*const u8` is invalid
 --> a6.rs:1:37
  |
1 | fn main() { let f = 1.5f64; let p = f as *const u8; println!("{:p}", p); }
  |                                     ^^^^^^^^^^^^^^

error: aborting due to 1 previous error

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

The repairs mirror the bool case. The numeric intent is a float-to-integer cast, squarely on the table:

Rust
fn main() { let f = 1.5f64; let n = f as u32; println!("{}", n); }
Output
1

The output is 1 - a float-to-integer cast rounds toward zero. The address intent takes the pointer from the float itself, through a reference: a reference coerces to a raw pointer to the same type, and sized raw pointers cast between each other:

Rust
fn main() { let f = 1.5f64; let p = &f as *const f64 as *const u8; println!("{}", p as usize == &f as *const f64 as usize); }
Output
true

The output is true: the byte pointer built through the reference holds the same address as the float pointer it was cast from. A char cast to a raw pointer fails identically and repairs the same way, through its code point or through its address - both are worked in the traps.

The sixth failure: a trait-object pointer cast to a different trait

Raw pointers between sized types cast freely, so it is natural to expect trait-object pointers to cast sideways too. They do not: the Reference allows a trait-object cast only when the principal trait stays the same, with auto traits removed or added under conditions, because the metadata of a trait-object pointer is a vtable for one specific trait:

Rust
trait Foo {}
trait Bar {}
impl Foo for i32 {}
impl Bar for i32 {}
fn main() { let x: i32 = 42; let p: *const dyn Foo = &x; let q: *const dyn Bar = p as *const dyn Bar; println!("{:p}", q); }
Output
error[E0606]: casting `*const dyn Foo` as `*const dyn Bar` is invalid
 --> a17.rs:5:82
  |
5 | fn main() { let x: i32 = 42; let p: *const dyn Foo = &x; let q: *const dyn Bar = p as *const dyn Bar; println!("{:p}", q); }
  |                                                                                  ^^^^^^^^^^^^^^^^^^^
  |
  = note: the trait objects may have different vtables

error: aborting due to 1 previous error

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

The note is the diagnosis: the two trait objects may have different vtables, so the Foo pointer's metadata cannot serve as a Bar pointer's metadata. The repair builds the second pointer from the value, where a fresh vtable is supplied by the coercion at the binding:

Rust
trait Foo {}
trait Bar {}
impl Foo for i32 {}
impl Bar for i32 {}
fn main() { let x: i32 = 42; let q: *const dyn Bar = &x; println!("{}", unsafe { *(q as *const i32) }); }
Output
42

The output is 42, read back through an i32 pointer cast from the Bar pointer. Two pointers to the same value, one per trait, each built from the value itself - never cast from each other.

The traps

Trap
Four more shapes draw E0606, three neighboring codes collect the rest of the cast mistakes, and one chained cast compiles while doing something other than what it looks like.

A char cast to a raw pointer is the fifth failure with a different scalar:

Rust
fn main() { let c = 'a'; let p = c as *const u8; println!("{:p}", p); }
Output
error[E0606]: casting `char` as `*const u8` is invalid
 --> a8.rs:1:34
  |
1 | fn main() { let c = 'a'; let p = c as *const u8; println!("{:p}", p); }
  |                                  ^^^^^^^^^^^^^^

error: aborting due to 1 previous error

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

Its numeric repair is the code point, under the char-to-integer row:

Rust
fn main() { let c = 'a'; let n = c as u8; println!("{}", n); }
Output
97

The output is 97. A reference cast to bool also draws E0606 - and here the compiler's own help misleads:

Rust
fn main() { let x = &5u8; let b = x as bool; println!("{}", b); }
Output
error[E0606]: casting `&u8` as `bool` is invalid
 --> a15.rs:1:35
  |
1 | fn main() { let x = &5u8; let b = x as bool; println!("{}", b); }
  |                                   ^^^^^^^^^
  |
help: dereference the expression
  |
1 | fn main() { let x = &5u8; let b = *x as bool; println!("{}", b); }
  |                                   +

error: aborting due to 1 previous error

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

The help proposes dereferencing, but the dereferenced u8 cast to bool is itself refused, under E0054 - an integer cast to bool is probed below and draws exactly that code. The working repair dereferences and compares:

Rust
fn main() { let x = &5u8; let b = *x != 0; println!("{}", b); }
Output
true

The output is true. And a &str cast straight to a byte pointer draws E0606 as well, fat pointer to thin:

Rust
fn main() { let s = "hi"; let p = s as *const u8; println!("{:p}", p); }
Output
error[E0606]: casting `&str` as `*const u8` is invalid
 --> b5.rs:1:35
  |
1 | fn main() { let s = "hi"; let p = s as *const u8; println!("{:p}", p); }
  |                                   ^^^^^^^^^^^^^^

error: aborting due to 1 previous error

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

The repair is a method, not a cast: str offers its byte pointer directly:

Rust
fn main() { let s = "hi"; let p = s.as_ptr(); println!("{}", unsafe { *p }); }
Output
104

The output is 104, the byte of h. Dereferencing a string reference before casting lands on a neighbor instead: *s as u32, where s is a &str, draws E0605, because the str it names is not a primitive type.

The chained cast is the dangerous one because it compiles. Routing a reference through a raw pointer to an integer is two legal casts - reference to pointer by coercion and cast, pointer to integer by the table - so the whole chain passes:

Rust
fn main() { let x = &7u8; let y: u32 = x as *const u8 as u32; println!("{}", y); }
Output
2017422864

The output is a large number that changes from run to run: it is the low half of the value's address, truncated into a u32, not the 7 the value holds. The Reference states the hazard directly - casting a raw pointer to an integer smaller than the pointer type may truncate the address, and usize avoids it. A chain of legal casts is not a conversion of the value; it is a conversion of the pointer. When the value is what is wanted, dereference - the first failure's repair - and when the address is what is wanted, cast the pointer to usize.

The neighbors, briefly, with their own repairs. An integer cast to a reference draws E0605, "non-primitive cast", because a reference is not a primitive target:

Rust
fn main() { let n = 5u32; let r = n as &u8; println!("{}", r); }
Output
error[E0605]: non-primitive cast: `u32` as `&u8`
 --> a9.rs:1:35
  |
1 | fn main() { let n = 5u32; let r = n as &u8; println!("{}", r); }
  |                                   ^^^^^^^^ 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 repair is not a conversion at all: borrow the value:

Rust
fn main() { let n = 5u8; let r = &n; println!("{}", r); }
Output
5

The output is 5. The same E0605 collects a unit, a tuple, an array, a Vec, a Box, a String, and a struct cast to an integer, and an integer cast to a String - in each, a side of the cast is a non-primitive type, and the repair is a field access, a method, or a constructor rather than a cast. A cast to char from anything but u8 draws E0604:

Rust
fn main() { let b = true; let c = b as char; println!("{}", c); }
Output
error[E0604]: only `u8` can be cast as `char`, not `bool`
 --> a13.rs:1:35
  |
1 | fn main() { let b = true; let c = b as char; println!("{}", c); }
  |                                   ^^^^^^^^^ invalid cast

error: aborting due to 1 previous error

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

That code also collects a u32 cast to char and a raw pointer cast to char; the only integer that casts to char is u8, and wider integers route through a fallible conversion or a u8 first. A cast to bool draws E0054:

Rust
fn main() { let x = 5u8; let p = &x as *const u8; let b = p as bool; println!("{}", b); }
Output
error[E0054]: cannot cast `*const u8` as `bool`
 --> a7.rs:1:59
  |
1 | fn main() { let x = 5u8; let p = &x as *const u8; let b = p as bool; println!("{}", b); }
  |                                                           ^^^^^^^^^ unsupported cast

error: aborting due to 1 previous error

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

A float and an integer cast to bool draw the same code. For a pointer, the bool the program usually wants is a comparison - against null, or of its address:

Rust
fn main() { let x = 5u8; let p = &x as *const u8; let n = p as usize; println!("{}", n > 0); }
Output
true

The output is true, from comparing the pointer's usize address against zero. Three refusal codes, three different reasons: the target is not primitive (E0605), the target is char and the source is not u8 (E0604), the target is bool (E0054) - and E0606 for the pairs that are primitive on both sides but absent from the table.

The controls confirm the table from the other side. A u8 casts to a char - the one integer that may:

Rust
fn main() { println!("{}", 'A' as u8); }
Output
65

The output is 65. A sized raw pointer casts to a raw pointer of another sized type with its address unchanged, a raw pointer casts to usize and back to the same value, an array reference casts to a pointer to its first element, and a wide slice pointer casts down to a thin element pointer, discarding the length on purpose:

Rust
fn main() { let s: &[i32] = &[1, 2, 3]; let p: *const [i32] = s as *const [i32]; let d: *const i32 = p as *const i32; println!("{}", unsafe { *d }); }
Output
1

The output is 1, the first element, reached through the thinned pointer. Numeric widening, bool and char to integers, an enum to its discriminant integer, a function item to a function pointer, and a float to an integer all compile as they stand - each is a row of the table, which is the whole test.

Fixes in reach order

When E0606 appears, work the pair in this order. First, ask which value the program needs. If it needs the pointed-to value, dereference before the cast - *x as u32 - and let the cast run on the value's own type. Second, if the source is text or a slice, name the number instead: parse the digits, or take the length, and cast only that integer. Third, if the target is a pointer, do not cast a scalar to it: take a pointer from a value that lives somewhere - through a reference and a sized-pointer cast - or, for strings, use the pointer method the type provides. Fourth, if the source is a trait-object pointer and the target is another trait, build the second pointer from the value so its vtable is supplied fresh. Fifth, check the code before repairing further: E0605 wants a field, method, or constructor instead of a cast; E0604 wants a u8 source for a char target; E0054 wants a comparison instead of a cast to bool. Last, audit any cast chain that compiles: each link may be legal while the chain converts an address instead of a value, and a pointer cast to an integer narrower than usize may be truncating it.

Decision summary

as performs only the casts on the Reference's table - numbers to numbers, a field-less enum, bool, or char to an integer, a u8 to a char, raw pointers to compatible raw pointers and to integer addresses, integers back to raw pointers, and function items to function pointers - plus ordinary coercions. Every other pair is refused: E0606 when both sides are cast-shaped types with no row between them, E0605 when a side is not primitive, E0604 when a char target is fed anything but a u8, and E0054 when the target is bool. The repairs never widen the cast; they change the operation - dereference to the value, parse or measure the text, take the pointer from a living value, build the second trait pointer from the value, or compare instead of casting. The compiler's dereference help is right for numeric targets and a way station for a bool target, where the dereferenced value draws the next code. And a chain of individually legal casts still converts only what its first link names: route a reference through a pointer and the integer at the end is an address, truncated if it is narrower than usize.

Checklist

  • The cast's pair appears on the Reference's cast table, or the expression is a coercion - anything else is one of the four refusal codes.
  • A reference was dereferenced before a numeric cast, so the cast runs on the value, not the address holder.
  • Text and slices contributed a named number - a parse or a length - never a cast of the reference itself.
  • A pointer target was reached from a living value, through a reference or a pointer method, never by casting a bool, float, or char.
  • A second trait-object pointer was built from the value, not cast from the first trait's pointer.
  • The refusal code was read before repairing: E0606, E0605, E0604, and E0054 each point at a different repair.
  • A cast to bool was replaced by a comparison; a cast to char starts from a u8.
  • Every cast chain was checked for what it actually converts, and pointer-to-integer casts use usize unless truncation is intended.

Sources

  • Rust error codes index, E0606 - an incompatible cast was attempted; only primitive types can be cast into each other, the index's &u8 as u32 example, and its dereference-first prescription.
  • The Rust Reference, Type cast expressions - the closed cast table, the numeric, enum, primitive-to-integer, u8-to-char, pointer-to-pointer, pointer-to-address, and address-to-pointer rows, the trait-object metadata compatibility rules, and the warning that a pointer cast to a smaller integer may truncate the address.
  • Rust error codes index, E0605 - a non-primitive cast, the code drawn when a side of the cast is a struct, string, tuple, or a reference used as a target.
  • Rust error codes index, E0604 - only u8 can be cast as char, the code drawn by casts to char from other types.
  • Rust error codes index, E0054 - a cast to bool is not supported, the code drawn by integer, float, and raw-pointer casts to bool.

Keep Exploring