-
Notifications
You must be signed in to change notification settings - Fork 13.5k
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
Merged
Merged
Import rust-guidelines #22331
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*~ | ||
*.bak | ||
*.swp | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
% Style Guidelines | ||
|
||
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. | ||
|
||
### 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? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
% Errors | ||
|
||
> **[FIXME]** Add some general text here. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?