Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Guides writing and reviewing idiomatic Rust using Apollo GraphQL's best practices handbook: ownership, errors, performance, testing, docs.
78% 96%+17 ptsof test cases passed
Passed 22 of 23 test cases with the skill, and 18 of 23 without it.
3 of 3 safety checks passed
Not used yet
Rust enforces a strict error handling approach, but how you handle them defines where your code feels ergonomic, consistent and safe - as opposed to cryptic and painful. This chapter dives into best practices for modeling and managing fallible operations across libraries and binaries.
Even if you decide to crash your application with
unwraporexpect, Rust forces you to declare that intentionally.
Result, avoid panic 🫨Rust has a powerful type that wraps fallible data, Result, this allows us to handle Error cases according to our needs and manage the state of the application based on that.
Result:fn divide(x: f64, y: f64) -> Result<f64, DivisionError> {
if y == 0.0 {
Err(DivisionError::DividedByZero)
} else {
Ok(x / y)
}
}
panic! only in unrecoverable conditions - typically tests, assertions, bugs or a need to crash the application for some explicit reason.panic! in appropriate conditions:
todo!, similar to panic, but alerts the compiler that you are aware that there is code missing.unreachable!, you have reasoned about the code block and are sure that condition xyz is not possible and if ever becomes possible you want to be alerted.unimplemented!, specially useful for alerting that a block is not yet implemented with a reason.unwrap/expect in ProductionAlthough expect is preferred to unwrap, as it can have context, they should be avoided in production code as there are smarter alternatives to them. Considering that, they should be used in the following scenarios:
unwrap/expect:Result (or Option) can have a predefined early return value in case of Result::Err, that doesn't need to know the Err value, use let Ok(..) = else { return ... } pattern, as it helps with flatten functions:let Ok(json) = serde_json::from_str(&input) else {
return Err(MyError::InvalidJson);
Skills that do similar work, with their benchmark result.
Next to this one in the catalogue, plus more from the same author.