Skip to content

Commit 9eeebba

Browse files
committed
Imports
Closes #24
1 parent ea2b1c1 commit 9eeebba

File tree

1 file changed

+127
-1
lines changed

1 file changed

+127
-1
lines changed

guide/items.md

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,4 +433,130 @@ before:
433433

434434
When writing extern items (such as `extern "C" fn`), always be explicit about
435435
the ABI. For example, write `extern "C" fn foo ...`, not `extern fn foo ...`, or
436-
`extern "C" { ... }.
436+
`extern "C" { ... }`.
437+
438+
439+
### Imports (`use` statements)
440+
441+
If an import can be formatted on one line, do so. There should be no spaces
442+
around braces.
443+
444+
```rust
445+
use a::b::c;
446+
use a::b::d::*;
447+
use a::b::{foo, bar, baz};
448+
```
449+
450+
451+
#### Large list imports
452+
453+
Prefer to use multiple imports rather than a multi-line import. However, tools
454+
should not split imports by default (they may offer this as an option).
455+
456+
If an import does require multiple lines, then break after the opening brace
457+
and before the closing brace, use a trailing comma, and block indent the names.
458+
459+
460+
```rust
461+
// Prefer
462+
foo::{long, list, of, imports};
463+
foo::{more, imports};
464+
465+
// If necessary
466+
foo::{
467+
long, list, of, imports, more,
468+
imports, // Note trailing comma
469+
};
470+
```
471+
472+
473+
#### Ordering of imports
474+
475+
A *group* of imports is a set of imports on the same or sequential lines. One or
476+
more blank lines or other items (e.g., a function) separate groups of imports.
477+
478+
Within a group of imports, imports must be sorted ascii-betically. Groups of
479+
imports must not be merged or re-ordered.
480+
481+
482+
E.g., input:
483+
484+
```rust
485+
use d;
486+
use c;
487+
488+
use b;
489+
use a;
490+
```
491+
492+
output:
493+
494+
```rust
495+
use c;
496+
use d;
497+
498+
use a;
499+
use b;
500+
```
501+
502+
Because of `macro_use`, attributes must also start a new group and prevent
503+
re-ordering.
504+
505+
Note that tools which only have access to syntax (such as Rustfmt) cannot tell
506+
which imports are from an external crate or the std lib, etc.
507+
508+
509+
#### Ordering list import
510+
511+
Names in a list import must be sorted ascii-betically, but with `self` and
512+
`super` first, and groups and glob imports last. This applies recursively. For
513+
example, `a::*` comes before `b::a` but `a::b` comes before `a::*`. E.g.,
514+
`use foo::bar::{a, b::c, b::d, b::d::{x, y, z}, b::{self, r, s}};`.
515+
516+
517+
#### Normalisation
518+
519+
Tools must make the following normalisations:
520+
521+
* `use a::self;` -> `use a;`
522+
* `use a::{};` ->
523+
* `use a::{b};` -> `use a::b;`
524+
525+
And must apply these recursively.
526+
527+
Tools must not otherwise merge or un-merge import lists or adjust glob imports
528+
(without an explicit option).
529+
530+
531+
#### Nested imports
532+
533+
If there are any nested imports in a list import, then use the multi-line form,
534+
even if the import fits on one line. Each nested import must be on its own line,
535+
but non-nested imports must be grouped on as few lines as possible.
536+
537+
For example,
538+
539+
```rust
540+
use a::b::{
541+
x, y, z,
542+
w::{...},
543+
u::{...},
544+
};
545+
```
546+
547+
548+
#### Merging/un-merging imports
549+
550+
An example:
551+
552+
```rust
553+
// Un-merged
554+
use a::b;
555+
use a::c::d;
556+
557+
// Merged
558+
use a::{b, c::d};
559+
```
560+
561+
Tools must not merge or un-merge imports by default. They may offer merging or
562+
un-merging as an option.

0 commit comments

Comments
 (0)