Skip to content

Commit 96bea5e

Browse files
committed
Import rust-guidlines
at rust-lang/rust-guidelines@16fa41b Fixes #19315
1 parent df54632 commit 96bea5e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2574
-1
lines changed

mk/docs.mk

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ RUSTBOOK = $(RPATH_VAR2_T_$(CFG_BUILD)_H_$(CFG_BUILD)) $(RUSTBOOK_EXE)
7373

7474
D := $(S)src/doc
7575

76-
DOC_TARGETS := trpl
76+
DOC_TARGETS := trpl style
7777
COMPILER_DOC_TARGETS :=
7878
DOC_L10N_TARGETS :=
7979

@@ -275,3 +275,9 @@ trpl: doc/book/index.html
275275
doc/book/index.html: $(RUSTBOOK_EXE) $(wildcard $(S)/src/doc/trpl/*.md) | doc/
276276
$(Q)rm -rf doc/book
277277
$(Q)$(RUSTBOOK) build $(S)src/doc/trpl doc/book
278+
279+
style: doc/style/index.html
280+
281+
doc/style/index.html: $(RUSTBOOK_EXE) $(wildcard $(S)/src/doc/style/*.md) | doc/
282+
$(Q)rm -rf doc/style
283+
$(Q)$(RUSTBOOK) build $(S)src/doc/style doc/style

src/doc/style/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Generated when running `gitbook build`
2+
_book/
3+
4+
*~
5+
*.bak
6+
*.swp
7+
.DS_Store

src/doc/style/README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
% Rust Guidelines [working title]
2+
3+
This document collects the emerging principles, conventions, abstractions, and
4+
best practices for writing Rust code.
5+
6+
Since Rust is evolving at a rapid pace, these guidelines are
7+
preliminary. The hope is that writing them down explicitly will help
8+
drive discussion, consensus and adoption.
9+
10+
Whenever feasible, guidelines provide specific examples from Rust's standard
11+
libraries.
12+
13+
For now, you can find a rendered snapshot at
14+
[http://aturon.github.io/](http://aturon.github.io/). After
15+
[some infrastructure work](https://github.com/aturon/rust-guidelines/issues/17), the snapshot will move somewhere more
16+
official.
17+
18+
### Building the document
19+
20+
Like http://rustbyexample.com/, this guidelines document is written in
21+
the [`gitbook`](https://github.com/GitbookIO/gitbook) style. It can be
22+
compiled with a prototype tool,
23+
[`rustbook`](https://github.com/aturon/rust-book) that provides a
24+
minimal subset of `gitbook`'s functionality on top of `rustdoc`.
25+
26+
### Guideline statuses
27+
28+
Every guideline has a status:
29+
30+
* **[FIXME]**: Marks places where there is more work to be done. In
31+
some cases, that just means going through the RFC process.
32+
33+
* **[FIXME #NNNNN]**: Like **[FIXME]**, but links to the issue tracker.
34+
35+
* **[RFC #NNNN]**: Marks accepted guidelines, linking to the rust-lang
36+
RFC establishing them.
37+
38+
### Guideline stabilization
39+
40+
One purpose of these guidelines is to reach decisions on a number of
41+
cross-cutting API and stylistic choices. Discussion and development of
42+
the guidelines will happen primarily on http://discuss.rust-lang.org/,
43+
using the Guidelines category. Discussion can also occur on the
44+
[guidelines issue tracker](https://github.com/rust-lang/rust-guidelines).
45+
46+
Guidelines that are under development or discussion will be marked with the
47+
status **[FIXME]**, with a link to the issue tracker when appropriate.
48+
49+
Once a concrete guideline is ready to be proposed, it should be filed
50+
as an [FIXME: needs RFC](https://github.com/rust-lang/rfcs). If the RFC is
51+
accepted, the official guidelines will be updated to match, and will
52+
include the tag **[RFC #NNNN]** linking to the RFC document.
53+
54+
### What's in this document
55+
56+
This document is broken into four parts:
57+
58+
* **[Style](style/README.md)** provides a set of rules governing naming conventions,
59+
whitespace, and other stylistic issues.
60+
61+
* **[Guidelines by Rust feature](features/README.md)** places the focus on each of
62+
Rust's features, starting from expressions and working the way out toward
63+
crates, dispensing guidelines relevant to each.
64+
65+
* **Topical guidelines and patterns**. The rest of the document proceeds by
66+
cross-cutting topic, starting with
67+
[Ownership and resources](ownership/README.md).
68+
69+
* **[APIs for a changing Rust](changing/README.md)**
70+
discusses the forward-compatibility hazards, especially those that interact
71+
with the pre-1.0 library stabilization process.
72+
73+
> **[FIXME]** Add cross-references throughout this document to the tutorial,
74+
> reference manual, and other guides.
75+
76+
> **[FIXME]** What are some _non_-goals, _non_-principles, or _anti_-patterns that
77+
> we should document?

src/doc/style/SUMMARY.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Summary
2+
3+
* [Style](style/README.md)
4+
* [Whitespace](style/whitespace.md)
5+
* [Comments](style/comments.md)
6+
* [Braces, semicolons, commas](style/braces.md)
7+
* [Naming](style/naming/README.md)
8+
* [Ownership variants](style/naming/ownership.md)
9+
* [Containers/wrappers](style/naming/containers.md)
10+
* [Conversions](style/naming/conversions.md)
11+
* [Iterators](style/naming/iterators.md)
12+
* [Imports](style/imports.md)
13+
* [Organization](style/organization.md)
14+
* [Guidelines by Rust feature](features/README.md)
15+
* [Let binding](features/let.md)
16+
* [Pattern matching](features/match.md)
17+
* [Loops](features/loops.md)
18+
* [Functions and methods](features/functions-and-methods/README.md)
19+
* [Input](features/functions-and-methods/input.md)
20+
* [Output](features/functions-and-methods/output.md)
21+
* [For convenience](features/functions-and-methods/convenience.md)
22+
* [Types](features/types/README.md)
23+
* [Conversions](features/types/conversions.md)
24+
* [The newtype pattern](features/types/newtype.md)
25+
* [Traits](features/traits/README.md)
26+
* [For generics](features/traits/generics.md)
27+
* [For objects](features/traits/objects.md)
28+
* [For overloading](features/traits/overloading.md)
29+
* [For extensions](features/traits/extensions.md)
30+
* [For reuse](features/traits/reuse.md)
31+
* [Common traits](features/traits/common.md)
32+
* [Modules](features/modules.md)
33+
* [Crates](features/crates.md)
34+
* [Ownership and resources](ownership/README.md)
35+
* [Constructors](ownership/constructors.md)
36+
* [Builders](ownership/builders.md)
37+
* [Destructors](ownership/destructors.md)
38+
* [RAII](ownership/raii.md)
39+
* [Cells and smart pointers](ownership/cell-smart.md)
40+
* [Errors](errors/README.md)
41+
* [Signaling](errors/signaling.md)
42+
* [Handling](errors/handling.md)
43+
* [Propagation](errors/propagation.md)
44+
* [Ergonomics](errors/ergonomics.md)
45+
* [Safety and guarantees](safety/README.md)
46+
* [Using unsafe](safety/unsafe.md)
47+
* [Library guarantees](safety/lib-guarantees.md)
48+
* [Testing](testing/README.md)
49+
* [Unit testing](testing/unit.md)
50+
* [FFI, platform-specific code](platform.md)
51+
* [APIs for a changing Rust](changing/README.md)
52+
* [Pre-1.0 changes](changing/pre-1-0.md)
53+
* [Post-1.0 changes](changing/post-1-0.md)
54+
* [Timing unclear](changing/unclear.md)

src/doc/style/changing/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
% API design for a changing Rust
2+
3+
A number of planned Rust features will drastically affect the API design
4+
story. This section collects some of the biggest features with concrete examples
5+
of how the API will change.

src/doc/style/changing/post-1-0.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
% Post-1.0 changes
2+
3+
### Higher-kinded types
4+
5+
* A trait encompassing both `Iterable<T>` for some fixed `T` and
6+
`FromIterator<U>` for _all_ `U` (where HKT comes in). The train
7+
could provide e.g. a default `map` method producing the same kind of
8+
the container, but with a new type parameter.
9+
10+
* **Monadic-generic programming**? Can we add this without deprecating
11+
huge swaths of the API (including `Option::map`, `option::collect`,
12+
`result::collect`, `try!` etc.

src/doc/style/changing/pre-1-0.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
% Pre-1.0 changes
2+
3+
### `std` facade
4+
5+
We should revisit some APIs in `libstd` now that the facade effort is complete.
6+
7+
For example, the treatment of environment variables in the new
8+
`Command` API is waiting on access to hashtables before being
9+
approved.
10+
11+
### Trait reform
12+
13+
Potential for standard conversion traits (`to`, `into`, `as`).
14+
15+
Probably many other opportunities here.
16+
17+
### Unboxed closures

src/doc/style/changing/unclear.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
% Changes with unclear timing
2+
3+
### Associated items
4+
5+
* Many traits that currently take type parameters should instead use associated
6+
types; this will _drastically_ simplify signatures in some cases.
7+
8+
* Associated constants would be useful in a few places, e.g. traits for
9+
numerics, traits for paths.
10+
11+
### Anonymous, unboxed return types (aka `impl Trait` types)
12+
13+
* See https://github.com/rust-lang/rfcs/pull/105
14+
15+
* Could affect API design in several places, e.g. the `Iterator` trait.
16+
17+
### Default type parameters
18+
19+
We are already using this in a few places (e.g. `HashMap`), but it's
20+
feature-gated.
21+
22+
### Compile-time function evaluation (CTFE)
23+
24+
https://github.com/mozilla/rust/issues/11621
25+
26+
### Improved constant folding
27+
28+
https://github.com/rust-lang/rust/issues/7834

src/doc/style/errors/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
% Errors
2+
3+
> **[FIXME]** Add some general text here.

src/doc/style/errors/ergonomics.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
% Ergonomic error handling
2+
3+
Error propagation with raw `Result`s can require tedious matching and
4+
repackaging. This tedium is largely alleviated by the `try!` macro,
5+
and can be completely removed (in some cases) by the "`Result`-`impl`"
6+
pattern.
7+
8+
### The `try!` macro
9+
10+
Prefer
11+
12+
```rust
13+
use std::io::{File, Open, Write, IoError};
14+
15+
struct Info {
16+
name: String,
17+
age: int,
18+
rating: int
19+
}
20+
21+
fn write_info(info: &Info) -> Result<(), IoError> {
22+
let mut file = File::open_mode(&Path::new("my_best_friends.txt"),
23+
Open, Write);
24+
// Early return on error
25+
try!(file.write_line(format!("name: {}", info.name).as_slice()));
26+
try!(file.write_line(format!("age: {}", info.age).as_slice()));
27+
try!(file.write_line(format!("rating: {}", info.rating).as_slice()));
28+
return Ok(());
29+
}
30+
```
31+
32+
over
33+
34+
```rust
35+
use std::io::{File, Open, Write, IoError};
36+
37+
struct Info {
38+
name: String,
39+
age: int,
40+
rating: int
41+
}
42+
43+
fn write_info(info: &Info) -> Result<(), IoError> {
44+
let mut file = File::open_mode(&Path::new("my_best_friends.txt"),
45+
Open, Write);
46+
// Early return on error
47+
match file.write_line(format!("name: {}", info.name).as_slice()) {
48+
Ok(_) => (),
49+
Err(e) => return Err(e)
50+
}
51+
match file.write_line(format!("age: {}", info.age).as_slice()) {
52+
Ok(_) => (),
53+
Err(e) => return Err(e)
54+
}
55+
return file.write_line(format!("rating: {}", info.rating).as_slice());
56+
}
57+
```
58+
59+
See
60+
[the `result` module documentation](http://static.rust-lang.org/doc/master/std/result/index.html#the-try!-macro)
61+
for more details.
62+
63+
### The `Result`-`impl` pattern [FIXME]
64+
65+
> **[FIXME]** Document the way that the `io` module uses trait impls
66+
> on `IoResult` to painlessly propagate errors.

src/doc/style/errors/handling.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
% Handling errors
2+
3+
### Use task isolation to cope with failure. [FIXME]
4+
5+
> **[FIXME]** Explain how to isolate tasks and detect task failure for recovery.
6+
7+
### Consuming `Result` [FIXME]

src/doc/style/errors/propagation.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
% Propagation
2+
3+
> **[FIXME]** We need guidelines on how to layer error information up a stack of
4+
> abstractions.
5+
6+
### Error interoperation [FIXME]
7+
8+
> **[FIXME]** Document the `FromError` infrastructure.

0 commit comments

Comments
 (0)