std-method

slice::split_at_checked

Return two shared views in Some if the boundary is valid, or None for an out-of-range boundary. This is useful for parsing variable-length inputs. Expected stdout (not executed): Some(([1, 2], [3])) None Common mistake: Rejecting a split at exactly the length; an empty right side is valid.

Formal Syntax & Structure

rust syntaxrust
slice.split_at_checked(mid)

Working Example

Example Snippetrust
fn main() {    let data = [1, 2, 3];    println!("{:?}", data.split_at_checked(2));    println!("{:?}", data.split_at_checked(4));}
Rust Reference