Skip to content

Commit 25c7856

Browse files
authored
Merge pull request #94 from rust-lang-nursery/arrays
guide - array literals and indexing
2 parents 5a313e1 + 3b3a3e3 commit 25c7856

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

guide/expressions.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,64 @@ when you need to split it into multiple lines; examples:
1515
move |arg1: i32, arg2: i32| -> i32 { expr1; expr2 }
1616
```
1717

18+
### Array literals
19+
20+
For simple array literals, avoid line breaking, no spaces around square
21+
brackets, contents of the array should be separated by commas and spaces. If
22+
using the repeating initialiser, there should be a space after the semicolon
23+
only. Apply the same rules if using the `vec!` or similar macros (always use
24+
square brackets here). Examples:
25+
26+
```rust
27+
fn main() {
28+
[1, 2, 3];
29+
vec![a, b, c, d];
30+
let a = [42; 10];
31+
}
32+
```
33+
34+
If a line must be broken, prefer breaking only after the `;`, if possible.
35+
Otherwise, follow the rules below for function calls. In any case, the contents
36+
of the initialiser should be block indented and there should be line breaks
37+
after the opening bracket and before the closing bracket:
38+
39+
```
40+
fn main() {
41+
[
42+
a_long_expression();
43+
1234567890
44+
]
45+
let x = [
46+
an_expression,
47+
another_expression,
48+
a_third_expression,
49+
];
50+
}
51+
```
52+
53+
54+
### Array accesses, indexing, and slicing.
55+
56+
No spaces around the square brackets, avoid breaking lines if possible, never
57+
break a line between the target expression and the opening bracket. If the
58+
indexing expression covers multiple lines, then it should be block indented and
59+
there should be newlines after the opening brackets and before the closing
60+
bracket. However, this should be avoided where possible.
61+
62+
Examples:
63+
64+
```rust
65+
fn main() {
66+
foo[42];
67+
&foo[..10];
68+
bar[0..100];
69+
foo[4 + 5 / bar];
70+
a_long_target[
71+
a_long_indexing_expression
72+
];
73+
}
74+
```
75+
1876
### Unary operations
1977

2078
Do not include a space between a unary op and its operand (i.e., `!x`, not

0 commit comments

Comments
 (0)