Skip to content

Commit 00b7534

Browse files
authored
Merge branch 'master' into Add-u128-and-i128-tokens
2 parents 7272b60 + f1002ea commit 00b7534

File tree

8 files changed

+99
-75
lines changed

8 files changed

+99
-75
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ rust:
44
- nightly
55

66
before_script:
7-
- (cargo install mdbook --git https://github.com/rust-lang-nursery/mdBook.git --force || true)
7+
- (cargo install mdbook --version =0.1.7)
88

99
script:
1010
- export PATH=$PATH:/home/travis/.cargo/bin && mdbook test

src/crates-and-source-files.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
> UTF8BOM : `\uFEFF`
1212
> SHEBANG : `#!` ~[`[` `\n`] ~`\n`<sup>*</sup>
1313
14-
Although Rust, like any other language, can be implemented by an interpreter as
15-
well as a compiler, the only existing implementation is a compiler,
16-
and the language has
17-
always been designed to be compiled. For these reasons, this section assumes a
18-
compiler.
14+
15+
> Note: Although Rust, like any other language, can be implemented by an
16+
> interpreter as well as a compiler, the only existing implementation is a
17+
> compiler,and the language has always been designed to be compiled. For these
18+
> reasons, this section assumes a compiler.
1919
2020
Rust's semantics obey a *phase distinction* between compile-time and
2121
run-time.[^phase-distinction] Semantic rules that have a *static
@@ -66,9 +66,17 @@ apply to the crate as a whole.
6666
#![warn(non_camel_case_types)]
6767
```
6868

69-
A crate that contains a `main` function can be compiled to an executable. If a
70-
`main` function is present, its return type must be `()`
71-
("[unit]") and it must take no arguments.
69+
A crate that contains a `main` [function] can be compiled to an executable. If a
70+
`main` function is present, it must take no arguments, must not declare any
71+
[trait or lifetime bounds], must not have any [where clauses], and its return
72+
type must be one of the following:
73+
74+
* `()`
75+
<!-- * `!` -->
76+
* `Result<T, E> where T: on this list, E: Error`
77+
78+
> Note: The implementation of which return types are allowed is determined by
79+
> the unstable [`Termination`] trait.
7280
7381
The optional [_UTF8 byte order mark_] (UTF8BOM production) indicates that the
7482
file is encoded in UTF8. It can only occur at the beginning of the file and
@@ -98,9 +106,13 @@ fn main() {
98106

99107
[module]: items/modules.html
100108
[module path]: paths.html
101-
[attributes]: items-and-attributes.html
109+
[attributes]: attributes.html
102110
[unit]: types.html#tuple-types
103111
[_InnerAttribute_]: attributes.html
104112
[_Item_]: items.html
105113
[_shebang_]: https://en.wikipedia.org/wiki/Shebang_(Unix)
106114
[_utf8 byte order mark_]: https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8
115+
[function]: items/functions.html
116+
[`Termination`]: ../std/process/trait.Termination.html
117+
[where clause]: items/where-clauses.html
118+
[trait or lifetime bounds]: trait-bounds.html

src/expressions/method-call-expr.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,6 @@ and function invocation.
9494
[array]: types.html#array-and-slice-types
9595
[trait objects]: types.html#trait-objects
9696
[disambiguate call]: expressions/call-expr.html#disambiguating-function-calls
97+
[disambiguating function call syntax]: expressions/call-expr.html#disambiguating-function-calls
9798
[dereference]: expressions/operator-expr.html#the-dereference-operator
9899
[methods]: items/associated-items.html#methods

src/expressions/operator-expr.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ let a = & & & & mut 10;
8787
The `*` (dereference) operator is also a unary prefix operator. When applied to
8888
a [pointer](types.html#pointer-types) it denotes the pointed-to location. If
8989
the expression is of type `&mut T` and `*mut T`, and is either a local
90-
variable, a (nested) field of a local variance or is a mutable [place
90+
variable, a (nested) field of a local variable or is a mutable [place
9191
expression], then the resulting memory location can be assigned to.
9292
Dereferencing a raw pointer requires `unsafe`.
9393

@@ -207,15 +207,17 @@ expression context][value expression] so are moved or copied.
207207
| `+` | Addition | | Addition | `std::ops::Add` |
208208
| `-` | Subtraction | | Subtraction | `std::ops::Sub` |
209209
| `*` | Multiplication | | Multiplication | `std::ops::Mul` |
210-
| `/` | Division | | Division | `std::ops::Div` |
210+
| `/` | Division* | | Division | `std::ops::Div` |
211211
| `%` | Remainder | | Remainder | `std::ops::Rem` |
212212
| `&` | Bitwise AND | Logical AND | | `std::ops::BitAnd` |
213213
| <code>&#124;</code> | Bitwise OR | Logical OR | | `std::ops::BitOr` |
214214
| `^` | Bitwise XOR | Logical XOR | | `std::ops::BitXor` |
215215
| `<<` | Left Shift | | | `std::ops::Shl` |
216-
| `>>` | Right Shift* | | | `std::ops::Shr` |
216+
| `>>` | Right Shift** | | | `std::ops::Shr` |
217217

218-
\* Arithmetic right shift on signed integer types, logical right shift on
218+
\* Integer division rounds towards zero.
219+
220+
\*\* Arithmetic right shift on signed integer types, logical right shift on
219221
unsigned integer types.
220222

221223
Here are examples of these operators being used.
@@ -345,10 +347,13 @@ well as the following additional casts. Here `*T` means either `*const T` or
345347
| `&[T; n]` | `*const T` | Array to pointer cast |
346348
| [Function pointer](types.html#function-pointer-types) | `*V` where `V: Sized` | Function pointer to pointer cast |
347349
| Function pointer | Integer | Function pointer to address cast |
350+
| Closure \*\* | Function pointer | Closure to function pointer cast |
348351

349352
\* or `T` and `V` are compatible unsized types, e.g., both slices, both the
350353
same trait object.
351354

355+
\*\* only for closures that do capture (close over) any local variables
356+
352357
### Semantics
353358

354359
* Numeric cast
@@ -390,7 +395,8 @@ same trait object.
390395
> &nbsp;&nbsp; | [_Expression_] `=` [_Expression_]
391396
392397
An _assignment expression_ consists of a [place expression] followed by an
393-
equals sign (`=`) and a [value expression].
398+
equals sign (`=`) and a [value expression]. Such an expression always has
399+
the [`unit` type].
394400

395401
Evaluating an assignment expression [drops](destructors.html) the left-hand
396402
operand, unless it's an uninitialized local variable or field of a local variable,

src/items/implementations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl Shape for Circle {
9494

9595
### Trait Implementation Coherence
9696

97-
A trait implementation is consider incoherent if either the orphan check fails
97+
A trait implementation is considered incoherent if either the orphan check fails
9898
or there are overlapping implementation instances.
9999

100100
Two trait implementations overlap when there is a non-empty intersection of the

src/items/traits.md

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ interface consists of [associated items], which come in three varieties:
99

1010
All traits define an implicit type parameter `Self` that refers to "the type
1111
that is implementing this interface". Traits may also contain additional type
12-
parameters. These type parameters (including `Self`) may be constrained by
13-
other traits and so forth as usual.
12+
parameters. These type parameters, including `Self`, may be constrained by
13+
other traits and so forth [as usual][generics].
1414

1515
Traits are implemented for specific types through separate [implementations].
1616

@@ -26,8 +26,7 @@ Generic items may use traits as [bounds] on their type parameters.
2626
## Generic Traits
2727

2828
Type parameters can be specified for a trait to make it generic. These appear
29-
after the trait name, using the same syntax used in [generic
30-
functions](items/functions.html#generic-functions).
29+
after the trait name, using the same syntax used in [generic functions].
3130

3231
```rust
3332
trait Seq<T> {
@@ -48,71 +47,72 @@ Object safe traits can be the base trait of a [trait object]. A trait is
4847
and
4948
* Be a [method] that does not use `Self` except in the type of the receiver.
5049
* It must not have any associated constants.
50+
* All supertraits must also be object safe.
5151

5252
## Supertraits
5353

54-
Trait bounds on `Self` are considered "supertraits". These are required to be
55-
acyclic. Supertraits are somewhat different from other constraints in that
56-
they affect what methods are available in the vtable when the trait is used as
57-
a [trait object]. Consider the following example:
54+
**Supertraits** are traits that are required to be implemented for a type to
55+
implement a specific trait. Furthermore, anywhere a [generic] or [trait object]
56+
is bounded by a trait, it has access to the associated items of its supertraits.
57+
58+
Supertraits are declared by trait bounds on the `Self` type of a trait and
59+
transitively the supertraits of the traits declared in those trait bounds. It is
60+
an error for a trait to be its own supertrait.
61+
62+
The trait with a supertrait is called a **subtrait** of its supertrait.
63+
64+
The following is an example of declaring `Shape` to be a supertrait of `Circle`.
5865

5966
```rust
6067
trait Shape { fn area(&self) -> f64; }
6168
trait Circle : Shape { fn radius(&self) -> f64; }
6269
```
6370

64-
The syntax `Circle : Shape` means that types that implement `Circle` must also
65-
have an implementation for `Shape`. Multiple supertraits are separated by `+`,
66-
`trait Circle : Shape + PartialEq { }`. In an implementation of `Circle` for a
67-
given type `T`, methods can refer to `Shape` methods, since the typechecker
68-
checks that any type with an implementation of `Circle` also has an
69-
implementation of `Shape`:
71+
And the following is the same example, except using [where clauses].
7072

7173
```rust
72-
struct Foo;
73-
7474
trait Shape { fn area(&self) -> f64; }
75-
trait Circle : Shape { fn radius(&self) -> f64; }
76-
impl Shape for Foo {
77-
fn area(&self) -> f64 {
78-
0.0
79-
}
80-
}
81-
impl Circle for Foo {
82-
fn radius(&self) -> f64 {
83-
println!("calling area: {}", self.area());
75+
trait Circle where Self: Shape { fn radius(&self) -> f64; }
76+
```
8477

85-
0.0
78+
This next example gives `radius` a default implementation using the `area`
79+
function from `Shape`.
80+
81+
```rust
82+
# trait Shape { fn area(&self) -> f64; }
83+
trait Circle where Self: Shape {
84+
fn radius(&self) -> f64 {
85+
// A = pi * r^2
86+
// so algebraically,
87+
// r = sqrt(A / pi)
88+
(self.area() /std::f64::consts::PI).sqrt()
8689
}
8790
}
88-
89-
let c = Foo;
90-
c.radius();
9191
```
9292

93-
In type-parameterized functions, methods of the supertrait may be called on
94-
values of subtrait-bound type parameters. Referring to the previous example of
95-
`trait Circle : Shape`:
93+
This next example calls a supertrait method on a generic parameter.
9694

9795
```rust
9896
# trait Shape { fn area(&self) -> f64; }
9997
# trait Circle : Shape { fn radius(&self) -> f64; }
100-
fn radius_times_area<T: Circle>(c: T) -> f64 {
101-
// `c` is both a Circle and a Shape
102-
c.radius() * c.area()
98+
fn print_area_and_radius<C: Circle>(c: C) {
99+
// Here we call the area method from the supertrait `Shape` of `Circle`.
100+
println!("Area: {}", c.area());
101+
println!("Radius: {}", c.radius());
103102
}
104103
```
105104

106-
Likewise, supertrait methods may also be called on trait objects.
105+
Similarly, here is an example of calling supertrait methods on trait objects.
107106

108107
```rust
109108
# trait Shape { fn area(&self) -> f64; }
110109
# trait Circle : Shape { fn radius(&self) -> f64; }
111-
# impl Shape for i32 { fn area(&self) -> f64 { 0.0 } }
112-
# impl Circle for i32 { fn radius(&self) -> f64 { 0.0 } }
113-
# let mycircle = 0i32;
114-
let mycircle = Box::new(mycircle) as Box<Circle>;
115-
let nonsense = mycircle.radius() * mycircle.area();
110+
# struct UnitCircle;
111+
# impl Shape for UnitCircle { fn area(&self) -> f64 { std::f64::consts::PI } }
112+
# impl Circle for UnitCircle { fn radius(&self) -> f64 { 1.0 } }
113+
# let circle = UnitCircle;
114+
let circle = Box::new(circle) as Box<dyn Circle>;
115+
let nonsense = circle.radius() * circle.area();
116116
```
117117

118118
[bounds]: trait-bounds.html
@@ -121,3 +121,7 @@ let nonsense = mycircle.radius() * mycircle.area();
121121
[RFC 255]: https://github.com/rust-lang/rfcs/blob/master/text/0255-object-safety.md
122122
[associated items]: items/associated-items.html
123123
[method]: items/associated-items.html#methods
124+
[implementations]: items/implementations.html
125+
[generics]: items/generics.html
126+
[where clauses]: items/generics.html#where-clauses
127+
[generic functions]: items/functions.html#generic-functions

src/tokens.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ evaluated (primarily) at compile time.
2020
#### Characters and strings
2121

2222
| | Example | `#` sets | Characters | Escapes |
23-
|----------------------------------------------|-----------------|------------|-------------|---------------------|
24-
| [Character](#character-literals) | `'H'` | `N/A` | All Unicode | [Quote](#quote-escapes) & [ASCII](#ascii-escapes) & [Unicode](#unicode-escapes) |
25-
| [String](#string-literals) | `"hello"` | `N/A` | All Unicode | [Quote](#quote-escapes) & [ASCII](#ascii-escapes) & [Unicode](#unicode-escapes) |
26-
| [Raw](#raw-string-literals) | `r#"hello"#` | `0...` | All Unicode | `N/A` |
27-
| [Byte](#byte-literals) | `b'H'` | `N/A` | All ASCII | [Quote](#quote-escapes) & [Byte](#byte-escapes) |
28-
| [Byte string](#byte-string-literals) | `b"hello"` | `N/A` | All ASCII | [Quote](#quote-escapes) & [Byte](#byte-escapes) |
29-
| [Raw byte string](#raw-byte-string-literals) | `br#"hello"#` | `0...` | All ASCII | `N/A` |
23+
|----------------------------------------------|-----------------|-------------|-------------|---------------------|
24+
| [Character](#character-literals) | `'H'` | 0 | All Unicode | [Quote](#quote-escapes) & [ASCII](#ascii-escapes) & [Unicode](#unicode-escapes) |
25+
| [String](#string-literals) | `"hello"` | 0 | All Unicode | [Quote](#quote-escapes) & [ASCII](#ascii-escapes) & [Unicode](#unicode-escapes) |
26+
| [Raw](#raw-string-literals) | `r#"hello"#` | 0 or more\* | All Unicode | `N/A` |
27+
| [Byte](#byte-literals) | `b'H'` | 0 | All ASCII | [Quote](#quote-escapes) & [Byte](#byte-escapes) |
28+
| [Byte string](#byte-string-literals) | `b"hello"` | 0 | All ASCII | [Quote](#quote-escapes) & [Byte](#byte-escapes) |
29+
| [Raw byte string](#raw-byte-string-literals) | `br#"hello"#` | 0 or more\* | All ASCII | `N/A` |
30+
31+
\* The number of `#`s on each side of the same literal must be equivalent
3032

3133
#### ASCII escapes
3234

@@ -142,7 +144,7 @@ Some additional _escapes_ are available in either character or non-raw string
142144
literals. An escape starts with a `U+005C` (`\`) and continues with one of the
143145
following forms:
144146

145-
* An _8-bit code point escape_ starts with `U+0078` (`x`) and is
147+
* A _7-bit code point escape_ starts with `U+0078` (`x`) and is
146148
followed by exactly two _hex digits_ with value up to `0x7F`. It denotes the
147149
ASCII character with value equal to the provided hex value. Higher values are
148150
not permitted because it is ambiguous whether they mean Unicode code points or
@@ -202,7 +204,7 @@ r##"foo #"# bar"##; // foo #"# bar
202204
> &nbsp;&nbsp; `b'` ( ASCII_FOR_CHAR | BYTE_ESCAPE ) `'`
203205
>
204206
> ASCII_FOR_CHAR :
205-
> &nbsp;&nbsp; _any ASCII (i.e. 0x00 to 0x7F), except_ `'`, `/`, \\n, \\r or \\t
207+
> &nbsp;&nbsp; _any ASCII (i.e. 0x00 to 0x7F), except_ `'`, `\`, \\n, \\r or \\t
206208
>
207209
> BYTE_ESCAPE :
208210
> &nbsp;&nbsp; &nbsp;&nbsp; `\x` HEX_DIGIT HEX_DIGIT
@@ -222,7 +224,7 @@ _number literal_.
222224
> &nbsp;&nbsp; `b"` ( ASCII_FOR_STRING | BYTE_ESCAPE | STRING_CONTINUE )<sup>\*</sup> `"`
223225
>
224226
> ASCII_FOR_STRING :
225-
> &nbsp;&nbsp; _any ASCII (i.e 0x00 to 0x7F), except_ `"`, `/` _and IsolatedCR_
227+
> &nbsp;&nbsp; _any ASCII (i.e 0x00 to 0x7F), except_ `"`, `\` _and IsolatedCR_
226228
227229
A non-raw _byte string literal_ is a sequence of ASCII characters and _escapes_,
228230
preceded by the characters `U+0062` (`b`) and `U+0022` (double-quote), and
@@ -324,10 +326,8 @@ literal_. The grammar for recognizing the two kinds of literals is mixed.
324326
> HEX_DIGIT : [`0`-`9` `a`-`f` `A`-`F`]
325327
>
326328
> INTEGER_SUFFIX :
327-
> &nbsp;&nbsp; &nbsp;&nbsp; `u8` | `u16` | `u32` | `u64` | `usize`
328-
> &nbsp;&nbsp; | `i8` | `i16` | `i32` | `i64` | `isize`
329-
330-
<!-- FIXME: u128 and i128 -->
329+
> &nbsp;&nbsp; &nbsp;&nbsp; `u8` | `u16` | `u32` | `u64` | `u128` | `usize`
330+
> &nbsp;&nbsp; | `i8` | `i16` | `i32` | `i64` | `i128` | `isize`
331331
332332
An _integer literal_ has one of four forms:
333333

src/types.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ The machine types are the following:
5757
[-(2^15), 2^15 - 1], [-(2^31), 2^31 - 1], [-(2^63), 2^63 - 1], and
5858
[-(2^127), 2^127 - 1] respectively.
5959

60-
* The IEEE 754-2008 `binary32` and `binary64` floating-point types: `f32` and
60+
* The IEEE 754-2008 "binary32" and "binary64" floating-point types: `f32` and
6161
`f64`, respectively.
6262

6363
### Machine-dependent integer types
@@ -545,7 +545,7 @@ A *trait object* is an opaque value of another type that implements a set of
545545
traits. The set of traits is made up of an [object safe] *base trait* plus any
546546
number of [auto traits].
547547

548-
Trait objects implement the base trait, its auto traits, and any super traits
548+
Trait objects implement the base trait, its auto traits, and any [supertraits]
549549
of the base trait.
550550

551551
Trait objects are written as the optional keyword `dyn` followed by a set of
@@ -597,8 +597,8 @@ behind some type of pointer; for example `&dyn SomeTrait` or
597597

598598
- a pointer to an instance of a type `T` that implements `SomeTrait`
599599
- a _virtual method table_, often just called a _vtable_, which contains, for
600-
each method of `SomeTrait` that `T` implements, a pointer to `T`'s
601-
implementation (i.e. a function pointer).
600+
each method of `SomeTrait` and its [supertraits] that `T` implements, a
601+
pointer to `T`'s implementation (i.e. a function pointer).
602602

603603
The purpose of trait objects is to permit "late binding" of methods. Calling a
604604
method on a trait object results in virtual dispatch at runtime: that is, a
@@ -712,3 +712,4 @@ impl Printable for String {
712712
[issue 33140]: https://github.com/rust-lang/rust/issues/33140
713713
[_PATH_]: paths.html
714714
[_LIFETIME_OR_LABEL_]: tokens.html#lifetimes-and-loop-labels
715+
[supertraits]: items/traits.html#supertraits

0 commit comments

Comments
 (0)