概要
- rustのコーディングルールのまとめ
- https://aturon.github.io
- 暫定項目とかFIXMEが多いので要更新
- 成熟してないのでガンガン変わりそう
- 更新リクエスト
コーディングルール
空白、改行
- 1行は100文字以内に収める
- インデントはスペース4つ、タブは使わない
- 行末とファイル末尾には空白、空行は入れない
- 演算子を使うときは空白を入れる
# [deprecated = "Use `bar` instead."]
fn foo(a: uint, b: uint) -> uint {
a + b
}
- コロン、カンマの後には空白を入れる
fn foo(a: Bar);
MyStruct { foo: 3, bar: 4 }
foo(bar, baz);
- 構造体や1行でブロックを書く場合など、ブレースの後に空白を入れる
spawn(proc() { do_something(); })
Point { x: 0.1, y: 0.3 }
- 関数の宣言で改行をする場合は最初の引数にインデントを合わせる
fn frobnicate(a: Bar, b: Bar,
c: Bar, d: Bar)
-> Bar {
...
}
fn foo<T: This,
U: That>(
a: Bar,
b: Bar)
-> Baz {
...
}
- 複数行の関数呼び出しのルールは宣言時のものに合わせる
- 最後の引数でブロックを使用する場合は同じ行にブレースを記述して開業する
- 改行後はインデントを1から始める
fn foo_bar(a: Bar, b: Bar,
c: |Bar|) -> Bar {
...
}
// Same line is fine:
foo_bar(x, y, |z| { z.transpose(y) });
// Indented body on new line is also fine:
foo_bar(x, y, |z| {
z.quux();
z.rotate(x)
})
コメント
- 一行コメントは //
// Wait for the main task to return, and set the process error code
// appropriately.
- 複数行は /* */
/*
* Wait for the main task to return, and set the process error code
* appropriately.
*/
- docコメントは ///
- Github markdownを使う
- 先頭行は説明
/// Sets up a default runtime configuration, given compiler-supplied arguments.
///
/// This function will block until the entire pool of M:N schedulers has
/// exited. This function also requires a local task to be available.
///
/// # Arguments
///
/// * `argc` & `argv` - The argument vector. On Unix this information is used
/// by `os::args`.
/// * `main` - The initial procedure to run inside of the M:N scheduling pool.
/// Once this procedure exits, the scheduling pool will begin to shut
/// down. The entire pool (and this function) will only return once
/// all child tasks have finished executing.
///
/// # Return value
///
/// The return value is used as the process return code. 0 on success, 101 on
/// error.
ブレース、セミコロン、カンマ
- ブレースは同じ行に書く
fn foo() {
...
}
fn frobnicate(a: Bar, b: Bar,
c: Bar, d: Bar)
-> Bar {
...
}
trait Bar {
fn baz(&self);
}
impl Bar for Baz {
fn baz(&self) {
...
}
}
frob(|x| {
x.transpose()
})
- returnするときはセミコロンをつける
fn foo() {
do_something();
if condition() {
return;
}
do_something_else();
}
命名
- 一覧
Item | Convention |
---|---|
Crates | snake_case (単語が好ましい) |
Modules | snake_case |
Types | CamelCase |
Traits | CamelCase |
Enum variants | CamelCase |
Functions | snake_case |
Methods | snake_case |
General constructors | new or with_more_details |
Conversion constructors | from_some_other_type |
Local variables | snake_case |
Static variables | SCREAMING_SNAKE_CASE |
Constant variables | SCREAMING_SNAKE_CASE |
Type parameters | concise CamelCase, usually single uppercase letter: T |
Lifetimes | short, lowercase: 'a |