Semua buku

Profesional

Aplikasi Tentang Pelatih Masuk Mulai membaca

Computer Science · Glosarium

Apa itu Result type, error propagation?

Dikenal juga sebagai: result type · error propagation

Definition 7.7 University Computer Science — Year 2 · Bab 7 — Enums, Pattern Matching and Error Handling

The result type Result<T, E> is the enum with the variants Ok(T), a success carrying its value, and Err(E), a failure carrying an error of type E. Error propagation is the return of a failure to the caller: the operator ? applied to a Result gives the value inside Ok, or returns Err(e) from the enclosing function at once, converting e with From::from into that function’s error type.

Two errors travelling back. Above, the missing file: ? converts the io::Error and returns it from read_log, and main reports it. Below, a missing temperature: ? in parse_line skips the remaining fields, and read_log, which matches instead of using ?, records the error and reads the next line.
Two errors travelling back. Above, the missing file: ? converts the io::Error and returns it from read_log, and main reports it. Below, a missing temperature: ? in parse_line skips the remaining fields, and read_log, which matches instead of using ?, records the error and reads the next line.
Baca dalam konteks →