When designing APIs and handling potentially missing or unavailable values, prefer returning optional types (Option
When designing APIs and handling potentially missing or unavailable values, prefer returning optional types (Option
Key practices:
None
instead of using unreachable!()
for cases that might become reachable in the futureOption<T>
for platform-specific values that may not be available everywhereunwrap()
which can panicExample from the discussions:
// Instead of this (can panic if plugin changes):
_ => unreachable!(),
// Prefer this (safe fallback):
_ => None,
// For platform-specific values:
// Instead of: Result<(ResourceId, IpAddr, IpAddr, Fd), NetError>
// Use: Result<(ResourceId, IpAddr, IpAddr, Option<Fd>), NetError>
// Handle errors instead of panicking:
// Instead of: parsed_source.specifier().to_file_path().unwrap()
// Use proper error handling with Result types
This approach makes code more robust, prevents runtime panics, and clearly communicates when values might be absent through the type system.
Enter the URL of a public GitHub repository