std-method

slice::first

Read the initial element, if there is one. Unlike direct indexing, this handles an empty sequence without panicking. Expected stdout (not executed): Some(7) None Common mistake: Unwrapping the result without knowing the sequence is nonempty.

Formal Syntax & Structure

rust syntaxrust
slice.first() -> Option<&T>

Working Example

Example Snippetrust
fn main() {    let empty: [i32; 0] = [];    println!("{:?} {:?}", [7, 8].first(), empty.first());}
Rust Reference