Rust

The ? operator

Në shqip: Operatori ?

ShpjegimiSQ

Pikëpyetja pas një Result ose Option e kthen gabimin menjëherë te thirrësi nëse ka gabim, përndryshe të jep vlerën. E zëvendëson match-in e gjatë dhe e bën kodin me gabime shumë të shkurtër.

EnglishEN

A question mark after a Result or Option returns the error straight to the caller if there is one, otherwise gives you the value. It replaces a long match and makes error-handling code very short.

Si ta mendosh

Si sekretari që, kur një dokument mungon, ta kthen dosjen menjëherë pa e lexuar më tej.

Lexoje në anglisht

Like a clerk who, when a document's missing, hands the file back immediately without reading further.

Shembull kodirust

fn lexo_moshen(t: &str) -> Result<u8, std::num::ParseIntError> {
    let m: u8 = t.trim().parse()?;   // del herët nëse dështon
    Ok(m)
}

Terma të lidhur