PingCAP Style Guide

Guide to writing idiomatic code at PingCAP and in the TiKV project.


Project maintained by pingcap Hosted on GitHub Pages — Theme by mattgraham

Formatting

Use Rustfmt: cargo fmt --all; do not customise rustfmt settings (rustfmt.toml), except for temporary work-arounds. You can install Rustfmt using rustup component add rustfmt.

Rustfmt is not complete, there is some code where Rustfmt will not reformat code. A common example is some bodies of macro_rules! items and some macro uses. In these cases, follow the Rust formatting style guide.

When writing macro uses which Rustfmt does not format, if the content is code-like, try to follow the formatting style guide. If the content is tabular, arrange the data in columns using spaces, not tabs, on multiple-of-four column positions.

Always use Unix line endings (\n).

Rationale

Imports

Rustfmt will format imports but permits several different styles.

Example:

use crate::{
    pd::client::{Cluster, RetryClient},
    util::GLOBAL_TIMER_HANDLE,
    Result,
};
use futures::{
    compat::Compat01As03,
    prelude::*,
    ready,
    task::{Context, Poll},
};
use std::{
    pin::Pin,
    sync::Arc,
    time::{Duration, Instant},
};

pub use self::Request;

// ...

#[cfg(test)]
mod test {
    use super::*;
    use tokio_timer::timer::Handle;

    // ...
}

Rationale

<< Introduction | Naming >>