Guide to writing idiomatic code at PingCAP and in the TiKV project.
Result
type (aliasing is ok).Option
to signal an error, an Option
is used where a value may or may not exist, but either state is correct.?
, not try!
(rationale: ecosystem convention to use modern Rust idioms).?
or Result
methods rather than match
(rationale: more concise and readable code).Debug
and Display
(rationale: makes debugging easier).std::error::Error
(rationale: interoperability).Sync
and Send
(rationale: it is common to pass errors across threads).
Error + Send + Sync + 'static
.Result
rather than panicking: this gives the caller the option of panicking if they want to.
?
to unwrap
.expect
with a short message to unwrap
.
unwrap
or expect
should only be used where it is impossible to fail (e.g., after a result has already been checked with is_ok
, although if let
is usually a better solution here), or where it is impossible to recover from failure (e.g., mutex poisoning).unwrap
(or expect
) can be freely used in tests.unwrap
or assert
.