Rust

Cast to Bool E0054 Fix

Rust will cast between many primitive types with as - an integer to a float, a char to a number, a bool to an integer - but never into bool. Writing x as bool for a number, a char, or a pointer stops with E0054, whatever the source type and whatever alias names it. The reason is that a cast into bool would have to invent a rule for which values count as true, and Rust leaves that choice to the programmer: compare with zero, test for null, or match the cases explicitly. This guide shows each failing shape, the neighbouring invalid casts that draw different codes, the casts that do compile, and the comparisons that replace the cast and run.

Written December 2, 2026 · Tested with Rust 1.99.0 · 16 min read

By the end of this guide
  • The rule underneath: no source type casts into bool
  • How to read the diagnostic
  • The first failure: the index example
  • Every numeric source draws the same code

Written December 2, 2026

Tested with Rust 1.99.0

What this guide is

Error E0054 means it is not allowed to cast to a bool. The error codes index puts it in one sentence: "It is not allowed to cast to a bool."

An as cast into bool fails for every non-bool source probed in this guide: integers, a float, a char, a raw pointer, and a usize returned by len(). This guide shows those shapes, the two alias shapes that draw the same code, the invalid casts that draw E0605 or E0606 instead, the casts that compile and run, and the comparisons that do the job the cast was reaching for.

Build every program in this guide with rustc --edition 2021. In the compiler transcripts, the file names are the bare names shown on the command line.

The rule underneath: no source type casts into bool

A cast has to be a mechanical conversion. For numbers into bool there is no single mechanical rule Rust is willing to pick: should only 1 be true, or every nonzero value, and what about a negative number or 1.5? Rather than choose, the language bans the cast outright and the diagnostic for numeric sources offers the usual choice explicitly - "compare with zero instead". For a char or a raw pointer there is not even that obvious comparison, and those diagnostics carry no help line, only the label "unsupported cast" - the comparison has to be chosen case by case, as the repairs below do with the NUL character and is_null.

The ban is one-directional. Casting out of bool is ordinary: true as i32 compiles and runs below, printing 1. And casting a bool to itself, b as bool, also compiles - the ban covers non-bool sources, not the word bool as a target in the abstract.

How to read the diagnostic

The header names both types: "cannot cast i32 as bool" - the source type changes with the value being cast, showing u8, f64, usize, char, or *const i32 in the shapes below. For numeric sources the span covers the whole cast expression and a help block rewrites it as a comparison with zero. For char and pointer sources the span carries the label "unsupported cast" and there is no help block. An alias changes neither form: an alias for bool as the target still draws E0054 with the same help, and an alias for i32 as the source still reports the source as i32.

One reading trap matters: the help for an f64 source rewrites the cast as x != 0, naming the integer 0. That is the compiler's generic help text, shown byte-for-byte below; the float repair that compiles and runs in this guide compares with 0.0.

The first failure: the index example

The index's erroneous example, verbatim:

Rust
#![allow(unused)]

fn main() {

let x = 5;

// Not allowed, won't compile

let x_is_nonzero = x as bool;

}
Output
error[E0054]: cannot cast `i32` as `bool`
 --> fail_index.rs:9:20
  |
9 | let x_is_nonzero = x as bool;
  |                    ^^^^^^^^^
  |
help: compare with zero instead
  |
9 - let x_is_nonzero = x as bool;
9 + let x_is_nonzero = x != 0;
  |

error: aborting due to 1 previous error

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

The example is a single integer variable cast to bool, and it draws exactly one E0054 - no second code hides in it. The index gives one corrected form: compare with zero (let x_is_nonzero = x != 0;). That repair is shown running below.

Every numeric source draws the same code

A u8 fails the same way as the index's i32:

Rust
fn main() {
    let x: u8 = 1;
    let b = x as bool;
    println!("{}", b);
}
Output
error[E0054]: cannot cast `u8` as `bool`
 --> fail_u8.rs:3:13
  |
3 |     let b = x as bool;
  |             ^^^^^^^^^
  |
help: compare with zero instead
  |
3 -     let b = x as bool;
3 +     let b = x != 0;
  |

error: aborting due to 1 previous error

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

A float fails too - there is no rounding or truncation rule that rescues it:

Rust
fn main() {
    let x: f64 = 1.5;
    let b = x as bool;
    println!("{}", b);
}
Output
error[E0054]: cannot cast `f64` as `bool`
 --> fail_f64.rs:3:13
  |
3 |     let b = x as bool;
  |             ^^^^^^^^^
  |
help: compare with zero instead
  |
3 -     let b = x as bool;
3 +     let b = x != 0;
  |

error: aborting due to 1 previous error

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

A literal needs no variable to fail; 5 as bool draws E0054 directly, with the source reported as i32:

Rust
fn main() {
    let b = 5 as bool;
    println!("{}", b);
}
Output
error[E0054]: cannot cast `i32` as `bool`
 --> fail_literal.rs:2:13
  |
2 |     let b = 5 as bool;
  |             ^^^^^^^^^
  |
help: compare with zero instead
  |
2 -     let b = 5 as bool;
2 +     let b = 5 != 0;
  |

error: aborting due to 1 previous error

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

Even 0 as bool fails. The value does not matter, because the cast itself is banned, not any particular conversion:

Rust
fn main() {
    let b = 0 as bool;
    println!("{}", b);
}
Output
error[E0054]: cannot cast `i32` as `bool`
 --> fail_zero_literal.rs:2:13
  |
2 |     let b = 0 as bool;
  |             ^^^^^^^^^
  |
help: compare with zero instead
  |
2 -     let b = 0 as bool;
2 +     let b = 0 != 0;
  |

error: aborting due to 1 previous error

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

A usize produced by an expression - here a Vec length - fails the same way, and the help rewrites that whole expression as a comparison:

Rust
fn main() {
    let v = vec![1, 2, 3];
    let b = v.len() as bool;
    println!("{}", b);
}
Output
error[E0054]: cannot cast `usize` as `bool`
 --> fail_usize_expr.rs:3:13
  |
3 |     let b = v.len() as bool;
  |             ^^^^^^^^^^^^^^^
  |
help: compare with zero instead
  |
3 -     let b = v.len() as bool;
3 +     let b = v.len() != 0;
  |

error: aborting due to 1 previous error

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

Casting in two steps does not help either: (x as i32) as bool still ends at bool, and the final cast draws E0054, with the header "cannot cast i32 as bool".

Aliases change nothing, in either position

An alias for bool used as the target is still bool, and the cast still fails - note the span is one character shorter, covering x as B, while the header still says bool:

Rust
type B = bool;

fn main() {
    let x = 5;
    let b = x as B;
    println!("{}", b);
}
Output
error[E0054]: cannot cast `i32` as `bool`
 --> fail_alias_target.rs:5:13
  |
5 |     let b = x as B;
  |             ^^^^^^
  |
help: compare with zero instead
  |
5 -     let b = x as B;
5 +     let b = x != 0;
  |

error: aborting due to 1 previous error

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

An alias for the source type changes nothing either; the header reports the resolved type, i32:

Rust
type Int = i32;

fn main() {
    let x: Int = 5;
    let b = x as bool;
    println!("{}", b);
}
Output
error[E0054]: cannot cast `i32` as `bool`
 --> fail_alias_source.rs:5:13
  |
5 |     let b = x as bool;
  |             ^^^^^^^^^
  |
help: compare with zero instead
  |
5 -     let b = x as bool;
5 +     let b = x != 0;
  |

error: aborting due to 1 previous error

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

Direct or aliased, in either position, the probed i32 source cast to bool is E0054.

Char and pointer sources: same code, no help

A char cast to bool draws E0054, but with the "unsupported cast" label and no help block - there is no zero to compare a character with in the compiler's generic advice:

Rust
fn main() {
    let c = 'a';
    let b = c as bool;
    println!("{}", b);
}
Output
error[E0054]: cannot cast `char` as `bool`
 --> fail_char.rs:3:13
  |
3 |     let b = c as bool;
  |             ^^^^^^^^^ unsupported cast

error: aborting due to 1 previous error

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

A raw pointer cast to bool lands in the same form:

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

error: aborting due to 1 previous error

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

Both shapes are E0054, so the code alone does not tell you a comparison-with-zero help is waiting; for these sources the repair is a test written for the type - != '\0' for a char, is_null for a pointer - as shown in the repairs.

The shapes that are not this error: E0605 and E0606

Other invalid casts keep their own codes. A non-primitive cast - a Vec cast to an integer - draws E0605, whose message explains that an as expression only converts between primitive types or coerces to a trait object:

Rust
fn main() {
    let v = vec![1, 2];
    let n = v as i32;
    println!("{}", n);
}
Output
error[E0605]: non-primitive cast: `Vec<i32>` as `i32`
 --> neighbour_vec_as_int.rs:3:13
  |
3 |     let n = v as i32;
  |             ^^^^^^^^ 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`.

An invalid cast between primitive types where the target is not bool draws E0606. A &str cast to i32:

Rust
fn main() {
    let s = "hi";
    let n = s as i32;
    println!("{}", n);
}
Output
error[E0606]: casting `&str` as `i32` is invalid
 --> neighbour_str_as_int_e0605.rs:3:13
  |
3 |     let n = s as i32;
  |             ^^^^^^^^
  |
  = help: cast through a raw pointer first

error: aborting due to 1 previous error

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

A bool cast to a raw pointer - the reverse direction from E0054, and a different code:

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

error: aborting due to 1 previous error

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

A float cast to a raw pointer draws E0606 as well, with the header "casting f64 as *const i32 is invalid". The pattern across the probed shapes: casting to bool from a non-bool primitive is E0054; other invalid primitive pairings are E0606; a non-primitive source is E0605.

What does work: casts out of bool, and the identity cast

Casting a bool to an integer is allowed, with true becoming 1:

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

Casting a bool to bool is allowed - the source is already a bool, so nothing is being converted into one:

Rust
fn main() {
    let b = true;
    let c = b as bool;
    println!("{}", c);
}
Output
true

The same identity cast through an alias for bool also compiles and runs, printing true - the alias target only drew E0054 above because the source there was an integer.

What does work: ordinary primitive casts

An integer cast to a float compiles and runs:

Rust
fn main() {
    let x = 5;
    let f = x as f64;
    println!("{}", f);
}
Output
5

A char cast to a number gives its code point - 'a' becomes 97:

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

A raw pointer cast to usize compiles, which is how a pointer can be compared numerically when that is really the intent:

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

bool as a target is the exception among primitive targets probed here, not a sign that as is generally restricted.

Cast and literal mistakes cluster together. For a struct literal used on a non-struct type, see Struct Literal on Non-Struct Type E0071 Fix. For assigning to something that is not a place expression, see Assign to Non-Place Expression E0070 Fix. For a field named twice in one literal, see Field Specified More Than Once E0062 Fix.

The repairs

Compare an integer with zero. This is the index's corrected form, and with x equal to 5 it prints true:

Rust
fn main() {
    let x = 5;
    let x_is_nonzero = x != 0;
    println!("{}", x_is_nonzero);
}
Output
true

The same comparison with x equal to 0 prints false - the comparison, unlike the banned cast, states exactly which value counts as false:

Rust
fn main() {
    let x = 0;
    let x_is_nonzero = x != 0;
    println!("{}", x_is_nonzero);
}
Output
false

Compare a u8 with zero. The repair matches the source type; here a u8 equal to 0 prints false:

Rust
fn main() {
    let x: u8 = 0;
    let b = x != 0;
    println!("{}", b);
}
Output
false

Compare a float with 0.0. The float repair uses a float zero, not the integer 0 shown in the compiler's generic help:

Rust
fn main() {
    let x: f64 = 1.5;
    let b = x != 0.0;
    println!("{}", b);
}
Output
true

Compare a char with the NUL character. If the intent was "is this character set", naming the character that counts as false makes the rule explicit; 'a' prints true:

Rust
fn main() {
    let c = 'a';
    let b = c != '\0';
    println!("{}", b);
}
Output
true

Test a pointer with is_null. A pointer's truth value is almost always "does it point anywhere", and is_null says exactly that; a pointer to a live value prints true:

Rust
fn main() {
    let x = 5;
    let p = &x as *const i32;
    let b = !p.is_null();
    println!("{}", b);
}
Output
true

Test a collection with is_empty instead of casting its length. The length cast failed above; the direct test runs and prints true for a three-element Vec:

Rust
fn main() {
    let v = vec![1, 2, 3];
    let b = !v.is_empty();
    println!("{}", b);
}
Output
true

Match zero explicitly when the rule is not simply nonzero. A match states both branches, and for 5 it prints true:

Rust
fn main() {
    let x = 5;
    let b = match x {
        0 => false,
        _ => true,
    };
    println!("{}", b);
}
Output
true

Fixes in reach order

First, read the two types in the header. If the target is bool and the source is one of the primitive types probed here - an integer, a float, a char, or a raw pointer - the code is E0054 and no change of source type, literal value, alias, or intermediate cast will make it compile - every one of those variants was probed and drew the same code. A non-primitive source cast to bool, such as a Vec, draws E0605 instead, and a &str cast to bool draws E0606.

Second, for a numeric source, replace the cast with a comparison with zero of the source's own type: x != 0 for integers, x != 0.0 for floats.

Third, for a char or pointer source, write the test the value actually needs: compare the char with the specific character that counts as false, or call is_null on the pointer.

Fourth, if the cast came from a length or a count, consider the direct test on the collection - is_empty - instead of a comparison on the number.

Last, leave the legal casts alone: bool to integer, bool to bool (directly or through an alias), integer to float, char to a number, and pointer to usize all compile and run. Do not rewrite them while fixing the E0054 next to them.

Decision summary

E0054 fired for every cast to bool from a non-bool source probed: the index example (i32), a u8, an f64, the literals 5 and 0, a usize from len(), a chained (x as i32) as bool, a char, and a raw pointer - and for both alias forms, an alias for bool as the target and an alias for i32 as the source. Numeric sources drew the "compare with zero instead" help; the char and pointer sources drew the "unsupported cast" label with no help. It did not fire for a Vec cast to i32, which drew E0605, nor for a &str cast to i32, a bool cast to a raw pointer, or an f64 cast to a raw pointer, which drew E0606. The shapes that compiled and ran were bool cast to i32 (printing 1), bool cast to bool directly and through an alias (printing true), an integer cast to f64, a char cast to u32 (printing 97), and a pointer cast to usize. The repairs that ran were comparing an integer with zero for both a nonzero value (printing true) and zero itself (printing false), comparing a u8 with zero, comparing a float with 0.0, comparing a char with the NUL character, testing a pointer with is_null, testing a Vec with is_empty, and matching zero explicitly.

Checklist

  • No non-bool value casts to bool with as - not an integer, a float, a char, a pointer, a literal, or a length, and not through an alias or an intermediate cast.
  • Numeric sources get the "compare with zero instead" help; char and pointer sources get "unsupported cast" and no help - same code, different diagnostic shape.
  • The ban is one-directional: bool casts out to integers freely, and bool to bool compiles.
  • Other invalid casts are different codes: a non-primitive source is E0605 (including a Vec cast to bool), an invalid primitive pairing whose target is not bool is E0606, and a &str cast to bool is also E0606.
  • Write the test explicitly: != 0 for integers, != 0.0 for floats, a named character for a char, is_null for a pointer, is_empty for a collection.
  • The compiler's help for a float source literally shows != 0; the float comparison that compiles uses 0.0.

Sources

  • Rust error codes index, E0054 - it is not allowed to cast to a bool; the erroneous example sets let x = 5; and writes let x_is_nonzero = x as bool;, and the corrected form compares with zero, let x_is_nonzero = x != 0;.
  • Rust error codes index, E0605 - a non-primitive cast was attempted; the code drawn by a Vec<i32> cast to i32.
  • Rust error codes index, E0606 - an invalid cast between primitive types; the code drawn by a &str cast to i32, a bool cast to a raw pointer, and an f64 cast to a raw pointer.

Keep Exploring