E0029 String Range Pattern Fix

Written December 2, 2026 · Tested with Rust 1.99.1 · 10 min read

By the end of this guide
  • The rule underneath: a range pattern checks its endpoints at compile time
  • How to read the diagnostic
  • The first failure: the index example
  • The same error with other endpoint types and in a let

Written December 2, 2026

Tested with Rust 1.99.1

What this guide is

Error E0029 means a range pattern was written with endpoints of a type that range patterns do not accept. The error codes index puts the reason plainly: "In a match expression, only numbers and characters can be matched against a range. This is because the compiler checks that the range is non-empty at compile-time, and is unable to evaluate arbitrary comparison functions."

In the shapes probed in this guide, E0029 fires for a range between two string literals, between two bool values, and between two byte-string literals, in a match and in a let. It does not fire for a range between two floats, two chars, two bytes, or two integers, in the probed shapes - those endpoints are numeric or char, so the range pattern is accepted. This guide shows the E0029 shapes, the neighboring shapes that are not this error, and the repairs.

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: a range pattern checks its endpoints at compile time

A range pattern has to prove, before the program runs, that its start is not past its end. The compiler can do that proof for numbers and characters, whose ordering it knows. It cannot do it for strings, whose ordering relation it states it cannot evaluate at compile time - so a string range is rejected as a pattern, even though the same comparison between the same strings is ordinary code in an if or a guard.

That split is the whole fix. Keep the comparison, move it out of the pattern: a guard or an if can compare strings with >= and <= at run time, and a range pattern can still be used on a number or character derived from the string, such as its length or its first character, in the probed shapes.

How to read the diagnostic

The E0029 header is the same in every probed shape: "only char and numeric types are allowed in range patterns". The span underlines both endpoints and the range operator, and each endpoint gets the same note naming its type: "this is of type &'static str but it should be char or numeric" for the string shapes, "this is of type bool but it should be char or numeric" for the bool shape, and "this is of type &'static [u8; 1] but it should be char or numeric" for the byte-string shape.

No second error rode along in any probed E0029 shape: every failing program below drew exactly one error[E0029], except the empty numeric range, which drew a different code instead, and the tuple-endpoint shape, which failed at parse time with no error code.

The first failure: the index example

The index's erroneous example, verbatim. Its second match, the guard version, is the page's own repair - it never gets to run here, because the first match aborts compilation:

Rust
#![allow(unused)]
fn main() {
let string = "salutations !";
match string {
    "hello" ..= "world" => {}
    _ => {}
}
match string {
    s if s >= "hello" && s <= "world" => {}
    _ => {}
}
}
Output
error[E0029]: only `char` and numeric types are allowed in range patterns
 --> fail_index.rs:5:5
  |
5 |     "hello" ..= "world" => {}
  |     -------^^^^^-------
  |     |           |
  |     |           this is of type `&'static str` but it should be `char` or numeric
  |     this is of type `&'static str` but it should be `char` or numeric

error: aborting due to 1 previous error

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

Exactly one E0029, and nothing else. The endpoints are in the right order and the comparison itself is meaningful for strings - the pattern form is what is refused.

The same error with other endpoint types and in a let

An exclusive string range fails the same way. "hello" .. "world" draws exactly one E0029, with the same two endpoint notes:

Rust
fn main() {
    let string = "salutations !";
    match string {
        "hello" .. "world" => { println!("in"); }
        _ => { println!("out"); }
    }
}
Output
error[E0029]: only `char` and numeric types are allowed in range patterns
 --> fail_str_exclusive.rs:4:9
  |
4 |         "hello" .. "world" => { println!("in"); }
  |         -------^^^^-------
  |         |          |
  |         |          this is of type `&'static str` but it should be `char` or numeric
  |         this is of type `&'static str` but it should be `char` or numeric

error: aborting due to 1 previous error

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

An owned String matched through as_str fails the same way, in the probed shape: the endpoints are still string literals, and the notes still name &'static str.

A bool range fails too. true ..= false on a bool draws exactly one E0029, with the endpoint notes naming bool:

Rust
fn main() {
    let b = true;
    match b {
        true ..= false => { println!("in"); }
        _ => { println!("out"); }
    }
}
Output
error[E0029]: only `char` and numeric types are allowed in range patterns
 --> fail_bool_range.rs:4:9
  |
4 |         true ..= false => { println!("in"); }
  |         ----^^^^^-----
  |         |        |
  |         |        this is of type `bool` but it should be `char` or numeric
  |         this is of type `bool` but it should be `char` or numeric

error: aborting due to 1 previous error

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

A byte-string range is not a byte range. b"a" ..= b"z" draws exactly one E0029, and the notes name the endpoint type as &'static [u8; 1] - an array reference, not a single byte. A range between two single bytes is a different, accepted shape, shown in the repairs.

The pattern's position does not save it. A string range in a let - let "hello" ..= "world" = s; - draws exactly one E0029 in the probed shape, with the same header and endpoint notes as the match shapes.

The shapes that are not this error

An empty numeric range looks close - a range the compiler rejects - but the endpoints are numeric, so the refusal is about their order, not their type. 1000 ..= 5 on a u32 draws exactly one E0030, "lower bound for range pattern must be less than or equal to upper bound", and no E0029:

Rust
fn main() {
    match 5u32 {
        1000 ..= 5 => { println!("in"); }
        _ => { println!("out"); }
    }
}
Output
error[E0030]: lower bound for range pattern must be less than or equal to upper bound
 --> fail_empty_numeric_range.rs:3:9
  |
3 |         1000 ..= 5 => { println!("in"); }
  |         ^^^^^^^^^^ lower bound larger than upper bound

error: aborting due to 1 previous error

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

The fix there is the endpoints' order, not a guard.

A float range is not this error either. In the probed shape, 1.0 ..= 2.0 on an f64 value compiled and ran, printing in - floats count as numeric for this rule. A range with tuple endpoints, (0, 0) ..= (9, 9), failed earlier still, at parse time: the compiler reported it expected =>, if, or | where it found ..=, with no error code at all, in the probed shape.

For a numeric range whose lower bound is past its upper bound, the neighboring code is E0030.

The repairs

Every repair below keeps the comparison the range pattern was trying to make, and moves it to a place that accepts it: a guard, an if, or a range pattern over a number or character taken from the value.

Use a guard to compare strings inside a match. This is the index page's own repair. The pattern binds the whole string to one name, and the guard does the two comparisons the range could not. It compiled and ran in the probed shape, printing in for both the index value and a second value run through the same code:

Rust
fn main() {
    let string = "salutations !";
    match string {
        s if s >= "hello" && s <= "world" => { println!("in"); }
        _ => { println!("out"); }
    }
    let other = "jello";
    match other {
        s if s >= "hello" && s <= "world" => { println!("in"); }
        _ => { println!("out"); }
    }
}
Output
in
in

The guard really does discriminate: a "zebra" value run through the same guard shape compiled and ran, printing out.

Use the same comparison in an if when there is no match to keep. In the probed shape it compiled and ran, printing in for the value between the two endpoints:

Rust
fn main() {
    let s = "jello";
    if s >= "hello" && s <= "world" {
        println!("in");
    } else {
        println!("out");
    }
}
Output
in

Match the exact strings when there are only a few. A string literal on its own is a legal pattern - it is the range between two of them that is refused. In the probed shape the exact-arms version compiled and ran, printing hello:

Rust
fn main() {
    let string = "hello";
    match string {
        "hello" => { println!("hello"); }
        "world" => { println!("world"); }
        _ => { println!("other"); }
    }
}
Output
hello

Range over a character when the test is really about one character. A char range is accepted in the probed shape - 'a' ..= 'z' on a char compiled and ran, printing lower:

Rust
fn main() {
    let c = 'm';
    match c {
        'a' ..= 'z' => { println!("lower"); }
        _ => { println!("other"); }
    }
}
Output
lower

Range over a number derived from the string. The string's length is a number, so a range pattern over it is accepted: in the probed shape, matching on the length of a two-character string against 1 ..= 3 compiled and ran, printing short. Matching on the string's first character against 'a' ..= 'z' likewise compiled and ran, printing lower.

Range over single bytes, not byte strings. In the probed shape, b'a' ..= b'z' on a single u8 byte compiled and ran, printing lower - the single-byte endpoints are numeric, where the byte-string endpoints in the failing shape were not.

Two more accepted shapes compiled and ran in the probes without becoming separate repairs above: an exclusive numeric range, 1 .. 10 on a u32, printed in, and the float range 1.0 ..= 2.0 printed in.

Fixes in reach order

First, read the endpoint notes. In the probed shapes they name the endpoint type the compiler refused - &'static str, bool, or &'static [u8; 1] - and state what it should be: char or numeric.

Second, if the comparison itself is the point, move it into a guard or an if. Strings compare with >= and <= in ordinary code; only the range pattern form is refused, in the probed shapes.

Third, if the value can be reduced to a number or a character first - its length, its first character, a single byte - range over that instead, and keep the range pattern.

Fourth, if there are only a few strings to recognize, drop the range and match each literal as its own arm.

Last, check the code before reaching for this fix. In the probed shapes, an empty numeric range drew E0030, not E0029, and its fix is the endpoints' order; a float range and a char range drew no error at all.

Decision summary

E0029 fired in the probed shapes for a range pattern between two string literals - the index example (exactly one E0029), an exclusive string range (exactly one E0029), an owned String matched through as_str (exactly one E0029), and a string range in a let (exactly one E0029) - for a range between two bool values (exactly one E0029), and for a range between two byte-string literals (exactly one E0029). It did not fire for a range between two floats, two chars, two single bytes, or two integers, or for an exclusive numeric range, in the probed shapes. It also did not fire for an empty numeric range, which drew exactly one E0030 instead, or for a tuple-endpoint range, which failed at parse time with no error code. The repairs that compiled and ran were a match guard comparing the string (printing in for both probed in-range values and out for a "zebra" value), the same comparison in an if (printing in), exact string arms (printing hello), a char range (printing lower), a numeric range over a string's length (printing short), a char range over a string's first character (printing lower), and a single-byte range (printing lower).

Checklist

  • E0029 is about the endpoints' type, in the probed shapes: a range pattern accepts only char and numeric endpoints.
  • The span notes name the refused endpoint type - &'static str, bool, or &'static [u8; 1] in the probed shapes; that type is what has to change or move.
  • A string comparison is legal in a guard or an if - only the range pattern form is refused, in the probed shapes.
  • Floats count as numeric in the probed shape: a float range pattern compiled and ran.
  • A byte-string range is E0029 in the probed shape, but a range between two single bytes compiled and ran.
  • An empty numeric range is E0030 in the probed shape, not E0029 - check the code before choosing a fix.

Sources

  • Rust error codes index, E0029 - only numbers and characters can be matched against a range, because the compiler checks that the range is non-empty at compile time; the erroneous example matches a string against a range between two string literals, and the page shows a guard as the more general version.
  • Rust error codes index, E0030 - the neighboring code for a range pattern whose lower bound is greater than its upper bound, drawn in the probed shape by an empty numeric range.

Keep Exploring