std-method

str::get

Try to borrow a range of text. For an invalid boundary or out-of-range interval, the range lookup returns None rather than panicking. Ordinary single-integer indexing is not supported. Expected stdout (not executed): ش invalid Common mistake: Using get(1) to request a scalar.

Formal Syntax & Structure

rust syntaxrust
fn get<I>(&self, i: I) -> Option<&I::Output> where I: SliceIndex<str>

Working Example

Example Snippetrust
fn main() {    let text = "AشB";    println!("{}", text.get(1..3).unwrap_or("invalid"));    println!("{}", text.get(1..2).unwrap_or("invalid"));}
Rust Reference