DioxusLabs/dioxus · error

Invalid utf8; you cannot split at a byte that is not a char

Error message

Invalid utf8; you cannot split at a byte that is not a char boundary

What it means

const_str's split_at tried to reinterpret the left byte slice as UTF-8 and failed because the requested index does not fall on a char boundary. The function's contract requires the index to be a boundary; passing one inside a multi-byte character triggers this panic in the const context.

Source

Thrown at packages/const-serialize/src/str.rs:147

        let new_len = len as usize + str_bytes.len();
        let mut i = 0;
        while i < str_bytes.len() {
            bytes[len as usize + i] = MaybeUninit::new(str_bytes[i]);
            i += 1;
        }
        Self {
            bytes,
            len: new_len as u32,
        }
    }

    /// Split the string at a byte index. The byte index must be a char boundary
    pub const fn split_at(self, index: usize) -> (Self, Self) {
        let (left, right) = self.bytes().split_at(index);
        let left = match std::str::from_utf8(left) {
            Ok(s) => s,
            Err(_) => {
                panic!("Invalid utf8; you cannot split at a byte that is not a char boundary")
            }
        };
        let right = match std::str::from_utf8(right) {
            Ok(s) => s,
            Err(_) => {
                panic!("Invalid utf8; you cannot split at a byte that is not a char boundary")
            }
        };
        (Self::new(left), Self::new(right))
    }

    /// Split the string at the last occurrence of a character
    pub const fn rsplit_once(&self, char: char) -> Option<(Self, Self)> {
        let str = self.as_str();
        let mut index = str.len() - 1;
        // First find the bytes we are searching for
        let (char_bytes, len) = char_to_bytes(char);
        let (char_bytes, _) = char_bytes.split_at(len);

View on GitHub (pinned to 24f6a829df)

Solutions

  1. Only split strings at a UTF-8 char boundary. Use char_indices() or floor_char_boundary() to find a valid index.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at packages/const-serialize/src/str.rs:147 when the library encounters an invalid state.

Common situations: See trigger scenarios.

Understand the failure class


AI-assisted analysis of DioxusLabs/dioxus@24f6a829df (2026-08-23). Data as JSON: /api/errors/e079e49568c51248. Report an issue: GitHub.