std-method

str::split_at_checked

Split at a byte offset, returning None when the offset is out of bounds or inside a scalar. The two slices borrow the original text. Available since Rust 1.80. Expected stdout (not executed): ش | A true Common mistake: Using the number of preceding scalars as the byte offset.

Formal Syntax & Structure

rust syntaxrust
fn split_at_checked(&self, mid: usize) -> Option<(&str, &str)>

Working Example

Example Snippetrust
fn main() {    let text = "شA";    let (left, right) = text.split_at_checked(2).unwrap();    println!("{left} | {right}");    println!("{}", text.split_at_checked(1).is_none());}
Rust Reference