Skip to content

Rollup of 7 pull requests #28913

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 45 commits into from
Oct 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
4a5e190
reference: Unsafe section does not fit under Functions
tshepang Sep 24, 2015
8e420e0
trpl: Clarify closure terminology
chills42 Oct 6, 2015
a78a874
possessive its
chills42 Oct 6, 2015
7895ec2
address review concerns
chills42 Oct 6, 2015
4d40dd3
Restore line endings in a test
steveklabnik Oct 7, 2015
d21b4f5
Use the correct mod name from the example
id4ho Oct 4, 2015
cbf97c3
style guide: suggest manual links to constructors
sourcefrog Oct 7, 2015
4119fc2
Fix "the the" typo and split a run-on sentence
Wallacoloo Sep 20, 2015
03dfe89
Fix a typo
ykomatsu Oct 7, 2015
8fee548
Add new error code
GuillaumeGomez Oct 6, 2015
a94f684
Add error explanation for E0515
GuillaumeGomez Oct 6, 2015
5d01556
Make `--explain` Handle Partial Error Codes
iwillspeak Oct 6, 2015
e84461a
Alter formatting for words in Option::cloned doc comment
frewsxcv Oct 7, 2015
2ba0c48
trpl: Use ordered list to release user from counting
JIghtuse Oct 7, 2015
1e0fbbd
trpl: Fix some bad wording in iterators subsection
JIghtuse Oct 7, 2015
49d2441
bench: rewrite nbody for better vectorization
cristicbz Oct 7, 2015
e802180
core: Derive Default for Wrapping<T>
cristicbz Oct 7, 2015
bbc2056
Comment on shadowing with patterns
steveklabnik Oct 7, 2015
17033a6
rustfmt part of libcore/fmt
mkpankov Oct 7, 2015
3fbbee6
rustfmt hash submodule
mkpankov Oct 7, 2015
11a7773
Fix comment gone astray
mkpankov Oct 7, 2015
18c66b5
Rollup merge of #28836 - jackwilsonv:patch-6, r=steveklabnik
steveklabnik Oct 7, 2015
f688c0e
Rollup merge of #28856 - chills42:master, r=steveklabnik
steveklabnik Oct 7, 2015
4b44296
Rollup merge of #28874 - GuillaumeGomez:error_code, r=Manishearth
steveklabnik Oct 7, 2015
55c2002
Rollup merge of #28876 - steveklabnik:oops, r=Gankro
steveklabnik Oct 7, 2015
5470a1c
Rollup merge of #28878 - sourcefrog:doc-links, r=steveklabnik
steveklabnik Oct 7, 2015
94755b2
Rollup merge of #28880 - Wallacoloo:book-5.14-typo, r=alexcrichton
steveklabnik Oct 7, 2015
4c8a0c0
Rollup merge of #28882 - ykomatsu:trpl, r=steveklabnik
steveklabnik Oct 7, 2015
c66ff93
Rollup merge of #28885 - frewsxcv:patch-25, r=alexcrichton
steveklabnik Oct 7, 2015
fb09639
Rollup merge of #28889 - JIghtuse:str_doc, r=steveklabnik
steveklabnik Oct 7, 2015
0fbf7ea
Rollup merge of #28896 - mkpankov:core-fmt, r=nrc
steveklabnik Oct 7, 2015
4d7eee1
trpl: mention deriving in traits section
panicbit Oct 8, 2015
7515514
trpl: link to derivable trait's docs
panicbit Oct 8, 2015
88506c2
reference: fix typos
tshepang Oct 8, 2015
88898e6
Auto merge of #28892 - cristicbz:default-wrapping, r=alexcrichton
bors Oct 8, 2015
bcd27eb
Auto merge of #28891 - cristicbz:new-nbody, r=alexcrichton
bors Oct 8, 2015
f18d1ad
Format code-like text in Iterator::cloned doc-comment
frewsxcv Oct 8, 2015
b75fe81
Auto merge of #28897 - steveklabnik:rollup, r=steveklabnik
bors Oct 8, 2015
95285c4
Auto merge of #28621 - tshepang:move-safety, r=steveklabnik
bors Oct 8, 2015
252c383
Rollup merge of #28872 - iwillspeak:master, r=Manishearth
steveklabnik Oct 8, 2015
ac541d3
Rollup merge of #28893 - steveklabnik:gh28687, r=nikomatsakis
steveklabnik Oct 8, 2015
8600610
Rollup merge of #28904 - panicbit:trpl-derive, r=steveklabnik
steveklabnik Oct 8, 2015
8ca1425
Rollup merge of #28905 - tshepang:typo, r=steveklabnik
steveklabnik Oct 8, 2015
17819aa
Rollup merge of #28908 - GuillaumeGomez:patch-2, r=Manishearth
steveklabnik Oct 8, 2015
1625c13
Rollup merge of #28910 - frewsxcv:patch-26, r=steveklabnik
steveklabnik Oct 8, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/doc/grammar.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ symbol : "::" | "->"
| ',' | ';' ;
```

Symbols are a general class of printable [token](#tokens) that play structural
Symbols are a general class of printable [tokens](#tokens) that play structural
roles in a variety of grammar productions. They are catalogued here for
completeness as the set of remaining miscellaneous printable tokens that do not
otherwise appear as [unary operators](#unary-operator-expressions), [binary
Expand Down
188 changes: 94 additions & 94 deletions src/doc/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ The two values of the boolean type are written `true` and `false`.

### Symbols

Symbols are a general class of printable [token](#tokens) that play structural
Symbols are a general class of printable [tokens](#tokens) that play structural
roles in a variety of grammar productions. They are catalogued here for
completeness as the set of remaining miscellaneous printable tokens that do not
otherwise appear as [unary operators](#unary-operator-expressions), [binary
Expand Down Expand Up @@ -984,99 +984,6 @@ The type parameters can also be explicitly supplied in a trailing
there is not sufficient context to determine the type parameters. For example,
`mem::size_of::<u32>() == 4`.

#### Unsafety

Unsafe operations are those that potentially violate the memory-safety
guarantees of Rust's static semantics.

The following language level features cannot be used in the safe subset of
Rust:

- Dereferencing a [raw pointer](#pointer-types).
- Reading or writing a [mutable static variable](#mutable-statics).
- Calling an unsafe function (including an intrinsic or foreign function).

##### Unsafe functions

Unsafe functions are functions that are not safe in all contexts and/or for all
possible inputs. Such a function must be prefixed with the keyword `unsafe` and
can only be called from an `unsafe` block or another `unsafe` function.

##### Unsafe blocks

A block of code can be prefixed with the `unsafe` keyword, to permit calling
`unsafe` functions or dereferencing raw pointers within a safe function.

When a programmer has sufficient conviction that a sequence of potentially
unsafe operations is actually safe, they can encapsulate that sequence (taken
as a whole) within an `unsafe` block. The compiler will consider uses of such
code safe, in the surrounding context.

Unsafe blocks are used to wrap foreign libraries, make direct use of hardware
or implement features not directly present in the language. For example, Rust
provides the language features necessary to implement memory-safe concurrency
in the language but the implementation of threads and message passing is in the
standard library.

Rust's type system is a conservative approximation of the dynamic safety
requirements, so in some cases there is a performance cost to using safe code.
For example, a doubly-linked list is not a tree structure and can only be
represented with reference-counted pointers in safe code. By using `unsafe`
blocks to represent the reverse links as raw pointers, it can be implemented
with only boxes.

##### Behavior considered undefined

The following is a list of behavior which is forbidden in all Rust code,
including within `unsafe` blocks and `unsafe` functions. Type checking provides
the guarantee that these issues are never caused by safe code.

* Data races
* Dereferencing a null/dangling raw pointer
* Reads of [undef](http://llvm.org/docs/LangRef.html#undefined-values)
(uninitialized) memory
* Breaking the [pointer aliasing
rules](http://llvm.org/docs/LangRef.html#pointer-aliasing-rules)
with raw pointers (a subset of the rules used by C)
* `&mut` and `&` follow LLVM’s scoped [noalias] model, except if the `&T`
contains an `UnsafeCell<U>`. Unsafe code must not violate these aliasing
guarantees.
* Mutating non-mutable data (that is, data reached through a shared reference or
data owned by a `let` binding), unless that data is contained within an `UnsafeCell<U>`.
* Invoking undefined behavior via compiler intrinsics:
* Indexing outside of the bounds of an object with `std::ptr::offset`
(`offset` intrinsic), with
the exception of one byte past the end which is permitted.
* Using `std::ptr::copy_nonoverlapping_memory` (`memcpy32`/`memcpy64`
intrinsics) on overlapping buffers
* Invalid values in primitive types, even in private fields/locals:
* Dangling/null references or boxes
* A value other than `false` (0) or `true` (1) in a `bool`
* A discriminant in an `enum` not included in the type definition
* A value in a `char` which is a surrogate or above `char::MAX`
* Non-UTF-8 byte sequences in a `str`
* Unwinding into Rust from foreign code or unwinding from Rust into foreign
code. Rust's failure system is not compatible with exception handling in
other languages. Unwinding must be caught and handled at FFI boundaries.

[noalias]: http://llvm.org/docs/LangRef.html#noalias

##### Behavior not considered unsafe

This is a list of behavior not considered *unsafe* in Rust terms, but that may
be undesired.

* Deadlocks
* Leaks of memory and other resources
* Exiting without calling destructors
* Integer overflow
- Overflow is considered "unexpected" behavior and is always user-error,
unless the `wrapping` primitives are used. In non-optimized builds, the compiler
will insert debug checks that panic on overflow, but in optimized builds overflow
instead results in wrapped values. See [RFC 560] for the rationale and more details.

[RFC 560]: https://github.com/rust-lang/rfcs/blob/master/text/0560-integer-overflow.md

#### Diverging functions

A special kind of function can be declared with a `!` character where the
Expand Down Expand Up @@ -4050,6 +3957,99 @@ In general, `--crate-type=bin` or `--crate-type=lib` should be sufficient for
all compilation needs, and the other options are just available if more
fine-grained control is desired over the output format of a Rust crate.

# Unsafety

Unsafe operations are those that potentially violate the memory-safety
guarantees of Rust's static semantics.

The following language level features cannot be used in the safe subset of
Rust:

- Dereferencing a [raw pointer](#pointer-types).
- Reading or writing a [mutable static variable](#mutable-statics).
- Calling an unsafe function (including an intrinsic or foreign function).

## Unsafe functions

Unsafe functions are functions that are not safe in all contexts and/or for all
possible inputs. Such a function must be prefixed with the keyword `unsafe` and
can only be called from an `unsafe` block or another `unsafe` function.

## Unsafe blocks

A block of code can be prefixed with the `unsafe` keyword, to permit calling
`unsafe` functions or dereferencing raw pointers within a safe function.

When a programmer has sufficient conviction that a sequence of potentially
unsafe operations is actually safe, they can encapsulate that sequence (taken
as a whole) within an `unsafe` block. The compiler will consider uses of such
code safe, in the surrounding context.

Unsafe blocks are used to wrap foreign libraries, make direct use of hardware
or implement features not directly present in the language. For example, Rust
provides the language features necessary to implement memory-safe concurrency
in the language but the implementation of threads and message passing is in the
standard library.

Rust's type system is a conservative approximation of the dynamic safety
requirements, so in some cases there is a performance cost to using safe code.
For example, a doubly-linked list is not a tree structure and can only be
represented with reference-counted pointers in safe code. By using `unsafe`
blocks to represent the reverse links as raw pointers, it can be implemented
with only boxes.

## Behavior considered undefined

The following is a list of behavior which is forbidden in all Rust code,
including within `unsafe` blocks and `unsafe` functions. Type checking provides
the guarantee that these issues are never caused by safe code.

* Data races
* Dereferencing a null/dangling raw pointer
* Reads of [undef](http://llvm.org/docs/LangRef.html#undefined-values)
(uninitialized) memory
* Breaking the [pointer aliasing
rules](http://llvm.org/docs/LangRef.html#pointer-aliasing-rules)
with raw pointers (a subset of the rules used by C)
* `&mut` and `&` follow LLVM’s scoped [noalias] model, except if the `&T`
contains an `UnsafeCell<U>`. Unsafe code must not violate these aliasing
guarantees.
* Mutating non-mutable data (that is, data reached through a shared reference or
data owned by a `let` binding), unless that data is contained within an `UnsafeCell<U>`.
* Invoking undefined behavior via compiler intrinsics:
* Indexing outside of the bounds of an object with `std::ptr::offset`
(`offset` intrinsic), with
the exception of one byte past the end which is permitted.
* Using `std::ptr::copy_nonoverlapping_memory` (`memcpy32`/`memcpy64`
intrinsics) on overlapping buffers
* Invalid values in primitive types, even in private fields/locals:
* Dangling/null references or boxes
* A value other than `false` (0) or `true` (1) in a `bool`
* A discriminant in an `enum` not included in the type definition
* A value in a `char` which is a surrogate or above `char::MAX`
* Non-UTF-8 byte sequences in a `str`
* Unwinding into Rust from foreign code or unwinding from Rust into foreign
code. Rust's failure system is not compatible with exception handling in
other languages. Unwinding must be caught and handled at FFI boundaries.

[noalias]: http://llvm.org/docs/LangRef.html#noalias

## Behavior not considered unsafe

This is a list of behavior not considered *unsafe* in Rust terms, but that may
be undesired.

* Deadlocks
* Leaks of memory and other resources
* Exiting without calling destructors
* Integer overflow
- Overflow is considered "unexpected" behavior and is always user-error,
unless the `wrapping` primitives are used. In non-optimized builds, the compiler
will insert debug checks that panic on overflow, but in optimized builds overflow
instead results in wrapped values. See [RFC 560] for the rationale and more details.

[RFC 560]: https://github.com/rust-lang/rfcs/blob/master/text/0560-integer-overflow.md

# Appendix: Influences

Rust is not a particularly original language, with design elements coming from
Expand Down
17 changes: 17 additions & 0 deletions src/doc/style/style/comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,20 @@ Use inner doc comments _only_ to document crates and file-level modules:
//!
//! The core library is a something something...
```

### Explain context.

Rust doesn't have special constructors, only functions that return new
instances. These aren't visible in the automatically generated documentation
for a type, so you should specifically link to them:

``` rust
/// An iterator that yields `None` forever after the underlying iterator
/// yields `None` once.
///
/// These can be created through
/// [`iter.fuse()`](trait.Iterator.html#method.fuse).
pub struct Fuse<I> {
// ...
}
```
4 changes: 2 additions & 2 deletions src/doc/trpl/advanced-linking.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ shells out to the system linker (`gcc` on most systems, `link.exe` on MSVC),
so it makes sense to provide extra command line
arguments, but this will not always be the case. In the future `rustc` may use
LLVM directly to link native libraries, in which case `link_args` will have no
meaning. You can achieve the same effect as the `link-args` attribute with the
meaning. You can achieve the same effect as the `link_args` attribute with the
`-C link-args` argument to `rustc`.

It is highly recommended to *not* use this attribute, and rather use the more
Expand Down Expand Up @@ -71,7 +71,7 @@ Dynamic linking on Linux can be undesirable if you wish to use new library
features on old systems or target systems which do not have the required
dependencies for your program to run.

Static linking is supported via an alternative `libc`, `musl`. You can compile
Static linking is supported via an alternative `libc`, [`musl`](http://www.musl-libc.org). You can compile
your own version of Rust with `musl` enabled and install it into a custom
directory with the instructions below:

Expand Down
45 changes: 24 additions & 21 deletions src/doc/trpl/closures.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
% Closures

Rust not only has named functions, but anonymous functions as well. Anonymous
functions that have an associated environment are called ‘closures’, because they
close over an environment. Rust has a really great implementation of them, as
we’ll see.
Sometimes it is useful to wrap up a function and _free variables_ for better
clarity and reuse. The free variables that can be used come from the
enclosing scope and are ‘closed over’ when used in the function. From this, we
get the name ‘closures’ and Rust provides a really great implementation of
them, as we’ll see.

# Syntax

Expand Down Expand Up @@ -34,7 +35,7 @@ assert_eq!(4, plus_two(2));
```

You’ll notice a few things about closures that are a bit different from regular
functions defined with `fn`. The first is that we did not need to
named functions defined with `fn`. The first is that we did not need to
annotate the types of arguments the closure takes or the values it returns. We
can:

Expand All @@ -44,14 +45,15 @@ let plus_one = |x: i32| -> i32 { x + 1 };
assert_eq!(2, plus_one(1));
```

But we don’t have to. Why is this? Basically, it was chosen for ergonomic reasons.
While specifying the full type for named functions is helpful with things like
documentation and type inference, the types of closures are rarely documented
since they’re anonymous, and they don’t cause the kinds of error-at-a-distance
problems that inferring named function types can.
But we don’t have to. Why is this? Basically, it was chosen for ergonomic
reasons. While specifying the full type for named functions is helpful with
things like documentation and type inference, the full type signatures of
closures are rarely documented since they’re anonymous, and they don’t cause
the kinds of error-at-a-distance problems that inferring named function types
can.

The second is that the syntax is similar, but a bit different. I’ve added spaces
here for easier comparison:
The second is that the syntax is similar, but a bit different. I’ve added
spaces here for easier comparison:

```rust
fn plus_one_v1 (x: i32) -> i32 { x + 1 }
Expand All @@ -63,8 +65,8 @@ Small differences, but they’re similar.

# Closures and their environment

Closures are called such because they ‘close over their environment’. It
looks like this:
The environment for a closure can include bindings from its enclosing scope in
addition to parameters and local bindings. It looks like this:

```rust
let num = 5;
Expand Down Expand Up @@ -197,9 +199,10 @@ frame. Without `move`, a closure may be tied to the stack frame that created
it, while a `move` closure is self-contained. This means that you cannot
generally return a non-`move` closure from a function, for example.

But before we talk about taking and returning closures, we should talk some more
about the way that closures are implemented. As a systems language, Rust gives
you tons of control over what your code does, and closures are no different.
But before we talk about taking and returning closures, we should talk some
more about the way that closures are implemented. As a systems language, Rust
gives you tons of control over what your code does, and closures are no
different.

# Closure implementation

Expand Down Expand Up @@ -288,9 +291,9 @@ isn’t interesting. The next part is:
# some_closure(1) }
```

Because `Fn` is a trait, we can bound our generic with it. In this case, our closure
takes a `i32` as an argument and returns an `i32`, and so the generic bound we use
is `Fn(i32) -> i32`.
Because `Fn` is a trait, we can bound our generic with it. In this case, our
closure takes a `i32` as an argument and returns an `i32`, and so the generic
bound we use is `Fn(i32) -> i32`.

There’s one other key point here: because we’re bounding a generic with a
trait, this will get monomorphized, and therefore, we’ll be doing static
Expand Down Expand Up @@ -452,7 +455,7 @@ autogenerated name.
The error also points out that the return type is expected to be a reference,
but what we are trying to return is not. Further, we cannot directly assign a
`'static` lifetime to an object. So we'll take a different approach and return
a "trait object" by `Box`ing up the `Fn`. This _almost_ works:
a trait object by `Box`ing up the `Fn`. This _almost_ works:

```rust,ignore
fn factory() -> Box<Fn(i32) -> i32> {
Expand Down
4 changes: 2 additions & 2 deletions src/doc/trpl/crates-and-modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,8 @@ What's going on here?
First, both `extern crate` and `use` allow renaming the thing that is being
imported. So the crate is still called "phrases", but here we will refer
to it as "sayings". Similarly, the first `use` statement pulls in the
`japanese::farewells` module from the crate, but makes it available as
`jp_farewells` as opposed to simply `farewells`. This can help to avoid
`japanese::greetings` module from the crate, but makes it available as
`ja_greetings` as opposed to simply `greetings`. This can help to avoid
ambiguity when importing similarly-named items from different places.

The second `use` statement uses a star glob to bring in _all_ symbols from the
Expand Down
14 changes: 7 additions & 7 deletions src/doc/trpl/iterators.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ loop is just a handy way to write this `loop`/`match`/`break` construct.
`for` loops aren't the only thing that uses iterators, however. Writing your
own iterator involves implementing the `Iterator` trait. While doing that is
outside of the scope of this guide, Rust provides a number of useful iterators
to accomplish various tasks. Before we talk about those, we should talk about a
Rust anti-pattern. And that's using ranges like this.
to accomplish various tasks. But first, a few notes about limitations of ranges.

Yes, we just talked about how ranges are cool. But ranges are also very
primitive. For example, if you needed to iterate over the contents of a vector,
you may be tempted to write this:
Ranges are very primitive, and we often can use better alternatives. Consider
following Rust anti-pattern: using ranges to emulate a C-style `for` loop. Let’s
suppose you needed to iterate over the contents of a vector. You may be tempted
to write this:

```rust
let nums = vec![1, 2, 3];
Expand Down Expand Up @@ -281,8 +281,8 @@ If you are trying to execute a closure on an iterator for its side effects,
just use `for` instead.

There are tons of interesting iterator adapters. `take(n)` will return an
iterator over the next `n` elements of the original iterator. Let's try it out with our infinite
iterator from before:
iterator over the next `n` elements of the original iterator. Let's try it out
with an infinite iterator:

```rust
for i in (1..).take(5) {
Expand Down
Loading