空白
线长
// Lines should never exceed 100 characters.
// Instead, just wrap on to the next line.
let bad_example = "So this sort of code should never really happen, because it's really hard to fit on the screen!";
缩进
// You should always use 4 spaces for indentation.
// Tabs are discouraged - if you can, set your editor to convert
// a tab into 4 spaces.
let x = vec![1, 3, 5, 6, 7, 9];
for item in x {
if x / 2 == 3 {
println!("{}", x);
}
}
尾随空白
应删除文件或行末尾的尾随空格。
二元运算符
// For clarity, always add a space when using binary operators, e.g.
// +, -, =, *
let bad=3+4;
let good = 3 + 4;
这也适用于属性,例如:
// Good:
#[deprecated = "Don't use my class - use Bar instead!"]
// Bad:
#[deprecated="This is broken"]
分号
// There is no space between the end of a statement
// and a semicolon.
let bad = Some("don't do this!") ;
let good: Option<&str> = None;
对齐结构字段
// Struct fields should **not** be aligned using spaces, like this:
pub struct Wrong {
pub x : i32,
pub foo: i64
}
// Instead, just leave 1 space after the colon and write the type, like this:
pub struct Right {
pub x: i32,
pub foo: i64
}
功能签名
// Long function signatures should be wrapped and aligned so that
// the starting parameter of each line is aligned
fn foo(example_item: Bar, another_long_example: Baz,
yet_another_parameter: Quux)
-> ReallyLongReturnItem {
// Be careful to indent the inside block correctly!
}
背带
// The starting brace should always be on the same line as its parent.
// The ending brace should be on its own line.
fn bad()
{
println!("This is incorrect.");
}
struct Good {
example: i32
}
struct AlsoBad {
example: i32 }