-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Import rust-guidelines #22331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Import rust-guidelines #22331
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Generated when running `gitbook build` | ||
_book/ | ||
|
||
*~ | ||
*.bak | ||
*.swp | ||
.DS_Store | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could probably be left out |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
% Rust Guidelines [working title] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style Guide? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dammit, I knew i missed something |
||
|
||
This document collects the emerging principles, conventions, abstractions, and | ||
best practices for writing Rust code. | ||
|
||
Since Rust is evolving at a rapid pace, these guidelines are | ||
preliminary. The hope is that writing them down explicitly will help | ||
drive discussion, consensus and adoption. | ||
|
||
Whenever feasible, guidelines provide specific examples from Rust's standard | ||
libraries. | ||
|
||
For now, you can find a rendered snapshot at | ||
[http://aturon.github.io/](http://aturon.github.io/). After | ||
[some infrastructure work](https://github.com/aturon/rust-guidelines/issues/17), the snapshot will move somewhere more | ||
official. | ||
|
||
### Building the document | ||
|
||
Like http://rustbyexample.com/, this guidelines document is written in | ||
the [`gitbook`](https://github.com/GitbookIO/gitbook) style. It can be | ||
compiled with a prototype tool, | ||
[`rustbook`](https://github.com/aturon/rust-book) that provides a | ||
minimal subset of `gitbook`'s functionality on top of `rustdoc`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, this probably needs to be edited and it may need a general pass as well in more places, but that may be able to wait. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eh actually maybe there isn't that much else! |
||
|
||
### Guideline statuses | ||
|
||
Every guideline has a status: | ||
|
||
* **[FIXME]**: Marks places where there is more work to be done. In | ||
some cases, that just means going through the RFC process. | ||
|
||
* **[FIXME #NNNNN]**: Like **[FIXME]**, but links to the issue tracker. | ||
|
||
* **[RFC #NNNN]**: Marks accepted guidelines, linking to the rust-lang | ||
RFC establishing them. | ||
|
||
### Guideline stabilization | ||
|
||
One purpose of these guidelines is to reach decisions on a number of | ||
cross-cutting API and stylistic choices. Discussion and development of | ||
the guidelines will happen primarily on http://discuss.rust-lang.org/, | ||
using the Guidelines category. Discussion can also occur on the | ||
[guidelines issue tracker](https://github.com/rust-lang/rust-guidelines). | ||
|
||
Guidelines that are under development or discussion will be marked with the | ||
status **[FIXME]**, with a link to the issue tracker when appropriate. | ||
|
||
Once a concrete guideline is ready to be proposed, it should be filed | ||
as an [FIXME: needs RFC](https://github.com/rust-lang/rfcs). If the RFC is | ||
accepted, the official guidelines will be updated to match, and will | ||
include the tag **[RFC #NNNN]** linking to the RFC document. | ||
|
||
### What's in this document | ||
|
||
This document is broken into four parts: | ||
|
||
* **[Style](style/README.md)** provides a set of rules governing naming conventions, | ||
whitespace, and other stylistic issues. | ||
|
||
* **[Guidelines by Rust feature](features/README.md)** places the focus on each of | ||
Rust's features, starting from expressions and working the way out toward | ||
crates, dispensing guidelines relevant to each. | ||
|
||
* **Topical guidelines and patterns**. The rest of the document proceeds by | ||
cross-cutting topic, starting with | ||
[Ownership and resources](ownership/README.md). | ||
|
||
* **[APIs for a changing Rust](changing/README.md)** | ||
discusses the forward-compatibility hazards, especially those that interact | ||
with the pre-1.0 library stabilization process. | ||
|
||
> **[FIXME]** Add cross-references throughout this document to the tutorial, | ||
> reference manual, and other guides. | ||
|
||
> **[FIXME]** What are some _non_-goals, _non_-principles, or _anti_-patterns that | ||
> we should document? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Summary | ||
|
||
* [Style](style/README.md) | ||
* [Whitespace](style/whitespace.md) | ||
* [Comments](style/comments.md) | ||
* [Braces, semicolons, commas](style/braces.md) | ||
* [Naming](style/naming/README.md) | ||
* [Ownership variants](style/naming/ownership.md) | ||
* [Containers/wrappers](style/naming/containers.md) | ||
* [Conversions](style/naming/conversions.md) | ||
* [Iterators](style/naming/iterators.md) | ||
* [Imports](style/imports.md) | ||
* [Organization](style/organization.md) | ||
* [Guidelines by Rust feature](features/README.md) | ||
* [Let binding](features/let.md) | ||
* [Pattern matching](features/match.md) | ||
* [Loops](features/loops.md) | ||
* [Functions and methods](features/functions-and-methods/README.md) | ||
* [Input](features/functions-and-methods/input.md) | ||
* [Output](features/functions-and-methods/output.md) | ||
* [For convenience](features/functions-and-methods/convenience.md) | ||
* [Types](features/types/README.md) | ||
* [Conversions](features/types/conversions.md) | ||
* [The newtype pattern](features/types/newtype.md) | ||
* [Traits](features/traits/README.md) | ||
* [For generics](features/traits/generics.md) | ||
* [For objects](features/traits/objects.md) | ||
* [For overloading](features/traits/overloading.md) | ||
* [For extensions](features/traits/extensions.md) | ||
* [For reuse](features/traits/reuse.md) | ||
* [Common traits](features/traits/common.md) | ||
* [Modules](features/modules.md) | ||
* [Crates](features/crates.md) | ||
* [Ownership and resources](ownership/README.md) | ||
* [Constructors](ownership/constructors.md) | ||
* [Builders](ownership/builders.md) | ||
* [Destructors](ownership/destructors.md) | ||
* [RAII](ownership/raii.md) | ||
* [Cells and smart pointers](ownership/cell-smart.md) | ||
* [Errors](errors/README.md) | ||
* [Signaling](errors/signaling.md) | ||
* [Handling](errors/handling.md) | ||
* [Propagation](errors/propagation.md) | ||
* [Ergonomics](errors/ergonomics.md) | ||
* [Safety and guarantees](safety/README.md) | ||
* [Using unsafe](safety/unsafe.md) | ||
* [Library guarantees](safety/lib-guarantees.md) | ||
* [Testing](testing/README.md) | ||
* [Unit testing](testing/unit.md) | ||
* [FFI, platform-specific code](platform.md) | ||
* [APIs for a changing Rust](changing/README.md) | ||
* [Pre-1.0 changes](changing/pre-1-0.md) | ||
* [Post-1.0 changes](changing/post-1-0.md) | ||
* [Timing unclear](changing/unclear.md) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
% API design for a changing Rust | ||
|
||
A number of planned Rust features will drastically affect the API design | ||
story. This section collects some of the biggest features with concrete examples | ||
of how the API will change. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
% Post-1.0 changes | ||
|
||
### Higher-kinded types | ||
|
||
* A trait encompassing both `Iterable<T>` for some fixed `T` and | ||
`FromIterator<U>` for _all_ `U` (where HKT comes in). The train | ||
could provide e.g. a default `map` method producing the same kind of | ||
the container, but with a new type parameter. | ||
|
||
* **Monadic-generic programming**? Can we add this without deprecating | ||
huge swaths of the API (including `Option::map`, `option::collect`, | ||
`result::collect`, `try!` etc. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
% Pre-1.0 changes | ||
|
||
### `std` facade | ||
|
||
We should revisit some APIs in `libstd` now that the facade effort is complete. | ||
|
||
For example, the treatment of environment variables in the new | ||
`Command` API is waiting on access to hashtables before being | ||
approved. | ||
|
||
### Trait reform | ||
|
||
Potential for standard conversion traits (`to`, `into`, `as`). | ||
|
||
Probably many other opportunities here. | ||
|
||
### Unboxed closures |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
% Changes with unclear timing | ||
|
||
### Associated items | ||
|
||
* Many traits that currently take type parameters should instead use associated | ||
types; this will _drastically_ simplify signatures in some cases. | ||
|
||
* Associated constants would be useful in a few places, e.g. traits for | ||
numerics, traits for paths. | ||
|
||
### Anonymous, unboxed return types (aka `impl Trait` types) | ||
|
||
* See https://github.com/rust-lang/rfcs/pull/105 | ||
|
||
* Could affect API design in several places, e.g. the `Iterator` trait. | ||
|
||
### Default type parameters | ||
|
||
We are already using this in a few places (e.g. `HashMap`), but it's | ||
feature-gated. | ||
|
||
### Compile-time function evaluation (CTFE) | ||
|
||
https://github.com/mozilla/rust/issues/11621 | ||
|
||
### Improved constant folding | ||
|
||
https://github.com/rust-lang/rust/issues/7834 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
% Errors | ||
|
||
> **[FIXME]** Add some general text here. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
% Ergonomic error handling | ||
|
||
Error propagation with raw `Result`s can require tedious matching and | ||
repackaging. This tedium is largely alleviated by the `try!` macro, | ||
and can be completely removed (in some cases) by the "`Result`-`impl`" | ||
pattern. | ||
|
||
### The `try!` macro | ||
|
||
Prefer | ||
|
||
```rust | ||
use std::io::{File, Open, Write, IoError}; | ||
|
||
struct Info { | ||
name: String, | ||
age: int, | ||
rating: int | ||
} | ||
|
||
fn write_info(info: &Info) -> Result<(), IoError> { | ||
let mut file = File::open_mode(&Path::new("my_best_friends.txt"), | ||
Open, Write); | ||
// Early return on error | ||
try!(file.write_line(format!("name: {}", info.name).as_slice())); | ||
try!(file.write_line(format!("age: {}", info.age).as_slice())); | ||
try!(file.write_line(format!("rating: {}", info.rating).as_slice())); | ||
return Ok(()); | ||
} | ||
``` | ||
|
||
over | ||
|
||
```rust | ||
use std::io::{File, Open, Write, IoError}; | ||
|
||
struct Info { | ||
name: String, | ||
age: int, | ||
rating: int | ||
} | ||
|
||
fn write_info(info: &Info) -> Result<(), IoError> { | ||
let mut file = File::open_mode(&Path::new("my_best_friends.txt"), | ||
Open, Write); | ||
// Early return on error | ||
match file.write_line(format!("name: {}", info.name).as_slice()) { | ||
Ok(_) => (), | ||
Err(e) => return Err(e) | ||
} | ||
match file.write_line(format!("age: {}", info.age).as_slice()) { | ||
Ok(_) => (), | ||
Err(e) => return Err(e) | ||
} | ||
return file.write_line(format!("rating: {}", info.rating).as_slice()); | ||
} | ||
``` | ||
|
||
See | ||
[the `result` module documentation](http://static.rust-lang.org/doc/master/std/result/index.html#the-try!-macro) | ||
for more details. | ||
|
||
### The `Result`-`impl` pattern [FIXME] | ||
|
||
> **[FIXME]** Document the way that the `io` module uses trait impls | ||
> on `IoResult` to painlessly propagate errors. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
% Handling errors | ||
|
||
### Use task isolation to cope with failure. [FIXME] | ||
|
||
> **[FIXME]** Explain how to isolate tasks and detect task failure for recovery. | ||
|
||
### Consuming `Result` [FIXME] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
% Propagation | ||
|
||
> **[FIXME]** We need guidelines on how to layer error information up a stack of | ||
> abstractions. | ||
|
||
### Error interoperation [FIXME] | ||
|
||
> **[FIXME]** Document the `FromError` infrastructure. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
er I meant could this whole file be remove?