Rust

E0604 Only u8 Cast as Char Fix

E0604 is a one-type rule: an as cast may produce a char only from a u8. Every other source type in this guide's probed set - wider integers, signed integers, bool, floats, even a raw pointer - fails with the same code, because a char must be a Unicode scalar value and only a u8 is guaranteed to be one. The repair depends on what your value really is: a byte goes through a checked conversion to u8 first, and a code point goes through char::from_u32, which returns None for the values no char can hold. This guide works each shape, shows the repairs running, and separates the neighboring cast errors E0605, E0606, and E0607.

Written November 30, 2026 · Tested with Rust 1.99.1 · 17 min read

By the end of this guide
  • The rule underneath: the cast table has exactly one row into char
  • How to read the diagnostic
  • The first failure: a u32 cast as char
  • The same failure from signed, wider, and non-integer sources

Written November 30, 2026

Tested with Rust 1.99.1

What this guide is

Error E0604 is a cast to char from the wrong source type. The error codes index opens with the cause - "A cast to char was attempted on a type other than u8" - and its erroneous example is the first failure in this guide: 0u32 as char. The index gives the reason in one sentence: "char is a Unicode Scalar Value, an integer value from 0 to 0xD7FF and 0xE000 to 0x10FFFF," and "Only u8 always fits in those ranges so only u8 may be cast to char."

The practical payoff is choosing the right repair, because the two repairs are not interchangeable. If your value is a byte, the fix is a checked conversion to u8 and then the cast - or char::from on the u8 directly. If your value is a code point that may exceed a byte, the fix is the index's own suggestion: "To allow larger values, use char::from_u32, which checks the value is valid." Going through as u8 on a code point compiles, but it silently truncates - one of the running programs below turns 321 into 'A'. This guide reproduces the error across the source types, shows both repairs running, and separates the three neighboring cast errors that share the same as syntax: E0605, E0606, and E0607.

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 has exactly one row into char

The Rust Reference's type cast expressions section lists every cast as may perform, and states the boundary plainly: "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." In that table, exactly one row has char as its target: source type u8, target char, described as "Casts to the char with the corresponding code point," with the examples 65u8 as char giving 'A' and 214u8 as char giving 'Ö'. There is no row from any wider or signed integer, from bool, or from a float into char - those casts are simply absent from the table, and E0604 is the error that reports the absence when the target is char.

The char documentation explains why the table is that narrow. A char is a Unicode scalar value: "any Unicode code point other than a surrogate code point," with code points "in the range 0 to 0x10FFFF, inclusive" and surrogates "in the range 0xD800 to 0xDFFF." A u8 spans 0 to 255, entirely inside the valid range and nowhere near the surrogate gap, so every u8 is a valid char and the cast never needs a runtime check. Every wider type contains values no char can hold - too large, negative, or inside the surrogate gap - so the language refuses the cast outright and pushes you to a checked conversion instead.

How to read the diagnostic

The header names both types: "only u8 can be cast as char, not u32", with the source type you used in the second slot. One span follows, labeled "invalid cast", underlining the whole cast expression. The help block varies by source type. For a u32 source the compiler suggests the repair directly - "consider using char::from_u32 instead" - with a rewritten line. For an i8 source the help instead reads "consider casting from u8 instead", with no rewrite. For the other signed sources, the wider unsigned sources, and the float sources in this guide's probed set, the help reads "consider using char::from_u32 instead (via a u32)" and points back at the span without a rewrite - the compiler is telling you the value has to become a u32 first, and that step is yours to make checked. In this guide's probed set, the bool cast and the raw-pointer cast carried no help block at all. Read the header's second type first: it tells you exactly which source type you have to convert away from.

The first failure: a u32 cast as char

This is the error index's own example, as a complete program:

Rust
fn main() {
    let _c = 0u32 as char;
}
Output
error[E0604]: only `u8` can be cast as `char`, not `u32`
 --> q1.rs:2:14
  |
2 |     let _c = 0u32 as char;
  |              ^^^^^^^^^^^^ invalid cast
  |
help: consider using `char::from_u32` instead
  |
2 -     let _c = 0u32 as char;
2 +     let _c = char::from_u32(0u32);
  |

error: aborting due to 1 previous error

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

The value is 0 - a perfectly good code point - and the cast still fails. E0604 is a rule about the source type, not about the value that happens to be in it: the compiler does not check whether this particular u32 would fit, because the type as a whole does not always fit. Note also what the suggested rewrite returns: char::from_u32 produces an Option<char>, not a char, so adopting the help verbatim changes the type of the expression - the None case has to be handled wherever the rewrite lands.

The same failure from signed, wider, and non-integer sources

A signed source fails identically, with the "(via a u32)" form of the help:

Rust
fn main() {
    let _c = 65i32 as char;
}
Output
error[E0604]: only `u8` can be cast as `char`, not `i32`
 --> q2.rs:2:14
  |
2 |     let _c = 65i32 as char;
  |              ^^^^^^^^^^^^^ invalid cast
  |
help: consider using `char::from_u32` instead (via a `u32`)
 --> q2.rs:2:14
  |
2 |     let _c = 65i32 as char;
  |              ^^^^^^^^^^^^^

error: aborting due to 1 previous error

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

So does the next wider unsigned type, even though every u16 value below 0xD800 would be a valid code point:

Rust
fn main() {
    let _c = 65u16 as char;
}
Output
error[E0604]: only `u8` can be cast as `char`, not `u16`
 --> q3.rs:2:14
  |
2 |     let _c = 65u16 as char;
  |              ^^^^^^^^^^^^^ invalid cast
  |
help: consider using `char::from_u32` instead (via a `u32`)
 --> q3.rs:2:14
  |
2 |     let _c = 65u16 as char;
  |              ^^^^^^^^^^^^^

error: aborting due to 1 previous error

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

And the rule is not limited to integers. A bool cast as char emits E0604 too - with no help block in the probed set - even though bool casts to integer types are in the Reference's table:

Rust
fn main() {
    let _c = true as char;
}
Output
error[E0604]: only `u8` can be cast as `char`, not `bool`
 --> q4.rs:2:14
  |
2 |     let _c = true as char;
  |              ^^^^^^^^^^^^ invalid cast

error: aborting due to 1 previous error

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

In this guide's probed set, the E0604 family covers every non-u8 integer type (i8, i16, i32, i64, i128, isize, u16, u32, u64, u128, usize), bool, both float types (f32, f64), and a raw pointer (*const u8) - each emitted E0604 alone, with the source type named in the header. The one source shape that did not emit E0604 is the reference, which is the first trap below.

The traps: neighbors that are not E0604

Trap
Three nearby cast shapes fail under different codes. The full guides for each are linked from the traps below.

Casting a reference as char is E0606, not E0604 - even when the reference points at a u8, the one type that could have cast. The E0606 page describes its error as "An incompatible cast was attempted," and its own example is a reference cast to an integer. Here the target is char and the code is still E0606:

Rust
fn main() {
    let x = 65u8;
    let _c = &x as char;
}
Output
error[E0606]: casting `&u8` as `char` is invalid
 --> q5.rs:3:14
  |
3 |     let _c = &x as char;
  |              ^^^^^^^^^^
  |
help: remove the unneeded borrow
  |
3 -     let _c = &x as char;
3 +     let _c = x as char;
  |

error: aborting due to 1 previous error

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

The help gives this one away: remove the borrow and the remaining x as char is the legal u8 cast. The same code covers the E0606 page's own shape, a reference cast to an integer - dereference first, then cast:

Rust
fn main() {
    let x = &0u8;
    let _y: u32 = x as u32;
}
Output
error[E0606]: casting `&u8` as `u32` is invalid
 --> q7.rs:3:19
  |
3 |     let _y: u32 = x as u32;
  |                   ^^^^^^^^
  |
help: dereference the expression
  |
3 |     let _y: u32 = *x as u32;
  |                   +

error: aborting due to 1 previous error

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

Both reference shapes are covered in the E0606 incompatible cast guide.

Casting to a type that is not a primitive at all is E0605. The E0605 page opens with "An invalid cast was attempted" and adds that "Only primitive types can be cast into each other":

Rust
fn main() {
    let x = 0u8;
    let _v = x as Vec<u8>;
}
Output
error[E0605]: non-primitive cast: `u8` as `Vec<u8>`
 --> q6.rs:3:14
  |
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`.

That shape - an as cast whose target is a struct, enum, or other non-primitive type - belongs to the E0605 invalid cast guide, not to this one: no conversion of the source value will make it castable, because the target itself is outside what as can produce.

Last, a cast between a thin pointer and a wide pointer is E0607 - the E0607 page opens with "A cast between a thin and a wide pointer was attempted":

Rust
fn main() {
    let v = core::ptr::null::<u8>();
    let _p = v as *const [u8];
}
Output
error[E0607]: cannot cast thin pointer `*const u8` to wide pointer `*const [u8]`
 --> q8.rs:3:14
  |
3 |     let _p = v as *const [u8];
  |              ^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

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

That shape is covered in the E0607 thin-to-fat pointer cast guide. The split across all four codes in the probed set is by the shape of the cast: a primitive source other than u8 aimed at char is E0604, a reference source is E0606 whatever the target, a non-primitive target is E0605, and a thin-to-wide pointer cast is E0607.

Fixes in reach order

First, when the value really is a u8, cast it directly - that is the one row the cast table allows. Both of the Reference's example values run here:

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

The output is V then A - 86 and 65 are the code points for those letters. The equivalent conversion form, char::from on a u8, produces the same character without the as cast:

Rust
fn main() {
    println!("{}", char::from(86u8));
}
Output
V

The output is V. Reach for this pair of repairs only when the value's type is, or has honestly become, a u8 - which is what the next repair does when the value starts out wider.

Second, when the value starts in a wider unsigned type but is meant to be a byte, convert it checked with u8::try_from and then cast. The conversion is where the range decision happens: in-range values go through, and out-of-range values come back as an error instead of a wrong character:

Rust
fn main() {
    let v: u32 = 86;
    let b = u8::try_from(v).unwrap();
    println!("{}", b as char);
    println!("{:?}", u8::try_from(256u32));
}
Output
V
Err(TryFromIntError(PosOverflow))

The output is V, then the error value for 256 - one past the largest u8. The unwrap() in this program is safe only because the input is the literal-shaped value 86 assigned on the line above; on a value that arrives from outside the program, handle the Err case instead of unwrapping it.

Do not shortcut this repair with a bare as u8. Chaining the casts compiles - integer-to-integer casts are in the table - but a numeric cast from a larger integer to a smaller one truncates, as the Reference states, and the result is a different character with no error anywhere:

Rust
fn main() {
    let v: u32 = 321;
    println!("{}", v as u8 as char);
}
Output
A

The output is A, code point 65 - because 321 truncated to 65 on the way through u8. The program the reader probably meant was about code point 321, 'Ł', and nothing in the output says otherwise. In the probed set, as u8 is a silent-truncation path, never a checked repair; use it only when truncation is genuinely the behavior wanted.

Third, when the value is a code point rather than a byte, use char::from_u32 - the error index's own suggestion, and the repair the compiler's help block names. It returns an Option, and the char documentation states the contract: "not all valid u32s are valid chars. from_u32() will return None if the input is not a valid value for a char." All three cases run in one program - a valid code point above the byte range, a surrogate, and a value past the maximum:

Rust
fn main() {
    println!("{:?}", char::from_u32(0x3B1));
    println!("{:?}", char::from_u32(0xD800));
    println!("{:?}", char::from_u32(0x110000));
}
Output
Some('α')
None
None

The output is Some('α') for 0x3B1 (945), then None twice: 0xD800 is the first surrogate code point, inside the gap the index describes, and 0x110000 is one past the largest code point, 0x10FFFF. Neither None is a failure of the repair - surfacing those two cases is exactly what the checked conversion is for, and the caller decides what a non-character means.

For a signed source, the route is two checked steps: convert to u32 with try_from, which rejects negatives, then let char::from_u32 validate the code point:

Rust
fn main() {
    let v: i32 = 945;
    println!("{:?}", u32::try_from(v).ok().and_then(char::from_u32));
    println!("{:?}", u32::try_from(-1i32));
}
Output
Some('α')
Err(TryFromIntError(NegOverflow))

The output is Some('α') - the same code point as the from_u32 program above, reached from a signed value - then the error for -1, which no u32 and therefore no char can hold. This is the "(via a u32)" path the compiler's help points at for this i32 source, with both steps checked.

Last, if the diagnostic is not E0604 at all, switch playbooks: a reference source is E0606 (dereference or drop the borrow first), a non-primitive target is E0605 (no as cast reaches it - use a conversion function or constructor instead), and a thin-to-wide pointer cast is E0607 (do not cast directly between thin and wide pointers), as the traps section shows.

Decision summary

E0604, "only u8 can be cast as char", is one shape in this guide's probed set: an as cast to char from a primitive source type other than u8 - every other integer type, bool, both float types, and a raw pointer each emitted E0604 alone, with the source type named in the header and a single "invalid cast" span. The diagnostic's help varies by source in the probed set: it points at char::from_u32 directly, with a rewrite, for a u32 source; reads "(via a u32)" for signed sources other than i8, for wider unsigned sources, and for float sources; reads "consider casting from u8 instead" for an i8 source; and is absent for the bool and raw-pointer casts. The repairs split by what the value is: a genuine u8 casts directly, or converts with char::from; a wider value meant to be a byte goes through u8::try_from first, never a bare as u8, which truncates silently in the probed set; a code point goes through char::from_u32, which returns None for surrogates and for values above 0x10FFFF in the probed set; a signed value takes the checked try_from step to u32 first. The neighbors are distinct: a reference cast is E0606 whatever its target, a cast to a non-primitive type is E0605, and a thin-to-wide pointer cast is E0607.

Checklist

  • Read the header's second type: in the probed set it names the exact source type that has to be converted away from.
  • Decide what the value is before repairing: a byte and a code point take different repairs, and the wrong one either fails to compile or truncates silently in the probed set.
  • Cast directly only from a genuine u8 - or use char::from on the u8.
  • For a wider value meant to be a byte, use u8::try_from and handle its Err case; do not shortcut with as u8, which truncated 321 to 'A' in the probed set.
  • For a code point, use char::from_u32 and handle the None case - surrogates and values above 0x10FFFF return None in the probed set.
  • For a signed value, convert with u32::try_from first; a negative value returns Err in the probed set.
  • If the code is not E0604, switch playbooks: in the probed set a reference source is E0606, a non-primitive target is E0605, and a thin-to-wide pointer cast is E0607.

Sources

  • Rust error codes index, E0604 - a cast to char was attempted on a type other than u8; the erroneous example 0u32 as char; char is a Unicode Scalar Value from 0 to 0xD7FF and 0xE000 to 0x10FFFF, only u8 always fits in those ranges, and char::from_u32 is the checked route for larger values, with the examples 86u8 as char giving 'V', char::from_u32(0x3B1) giving Some('α'), and char::from_u32(0xD800) giving None.
  • The Rust Reference, Type cast expressions - as performs coercions and the casts in its table, and any other cast is a compiler error; the table's single row into char is from u8, casting to the char with the corresponding code point (65u8 as char is 'A', 214u8 as char is 'Ö'); bool and char cast to integer types; casting from a larger integer to a smaller integer truncates.
  • Rust standard library, char - a char is a Unicode scalar value, any code point other than a surrogate code point, in the range 0 to 0x10FFFF with surrogates at 0xD800 to 0xDFFF; char::from_u32 converts a u32 to a char and returns None if the input is not a valid value for a char.
  • Rust error codes index, E0605 - an invalid cast was attempted; only primitive types can be cast into each other, with the erroneous examples u8 as Vec<u8> and a raw pointer as a reference.
  • Rust error codes index, E0606 - an incompatible cast was attempted; the erroneous example casts &u8 as u32, repaired by dereferencing first and then casting.
  • Rust error codes index, E0607 - a cast between a thin and a wide pointer was attempted; thin pointers are purely a reference to a memory address, wide pointers reference dynamically sized types and carry additional information such as a slice's size; the fix is not to cast directly between thin and wide pointers.

Keep Exploring