LEARN VOID / 08
Results & errors
Make recoverable outcomes explicit.
Complete program for the synchronous entry adapter. The result below is the returned integer.
func average(total: i32, count: i32) -> result<i32, string>:
if count <= 0:
return Err("at least one reading is required")
return Ok(total / count)
func entry(reading_count: i32) -> i32:
return match average(24, reading_count):
Ok(var mean): mean
Err(var message): 0Return a recoverable outcome
average divides a total by a reading count. result<i32, string> carries either an integer average via Ok or an explanatory error via Err. Counts of zero or less are rejected before division.
Handle both cases
The match captures success with Ok(var mean). For four readings totaling 24, the result is 6. This tiny CLI adapter deliberately uses zero as the application fallback when no valid average can be computed; a user-facing application would decide how to present message. This fallback is a policy of the example, not part of the result type.
Propagate when appropriate
In a compatible result-returning function, ? unwraps success or propagates the exact matching error type. Recoverable errors remain distinct from runtime failures, such as invalid indexing or a failed checked conversion.