Skip to content

Commit 305c436

Browse files
authored
Merge branch 'master' into fix-72
2 parents 779b2ce + 422cbb0 commit 305c436

13 files changed

+208
-114
lines changed

src/attributes.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,17 @@ macro scope.
184184
object file that this item's contents will be placed into.
185185
- `no_mangle` - on any item, do not apply the standard name mangling. Set the
186186
symbol for this item to its identifier.
187-
- `doc` - Doc comments such as `/// foo` are equivalent to `#[doc = "foo"]`.
188187
- `must_use` - on structs and enums, will warn if a value of this type isn't used or
189188
assigned to a variable. You may also include an optional message by using
190189
`#[must_use = "message"]` which will be given alongside the warning.
191190

191+
### Documentation
192+
193+
The `doc` attribute is used to document items and fields. [Doc comments]
194+
are transformed into `doc` attributes.
195+
196+
See [The Rustdoc Book] for reference material on this attribute.
197+
192198
### Conditional compilation
193199

194200
Sometimes one wants to have different compiler outputs from the same code,
@@ -408,4 +414,6 @@ impl<T: PartialEq> PartialEq for Foo<T> {
408414

409415
You can implement `derive` for your own type through [procedural macros].
410416

411-
[procedural macros]: procedural-macros.html
417+
[Doc comments]: comments.html#doc-comments
418+
[The Rustdoc Book]: ../rustdoc/the-doc-attribute.html
419+
[procedural macros]: procedural-macros.html

src/expressions/method-call-expr.md

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,22 @@ let log_pi = pi.unwrap_or(1.0).log(2.72);
1515
```
1616

1717
When resolving method calls on an expression of type `A`, Rust will use the
18-
following order, only looking at methods that are
19-
[visible](visibility-and-privacy.html). If the type of `A` is a type parameter
20-
or `Self` in a trait definitition then steps 2-4 first consider traits from
21-
bounds on the type paramter, then the traits that are in scope. For other
22-
types, only the traits that are in scope are considered.
18+
following order, only looking at methods that are [visible]. If the type of `A`
19+
is a type parameter or `Self` in a trait definitition then steps 2-4 first
20+
consider traits from bounds on the type paramter, then the traits that are in
21+
scope. For other types, only the traits that are in scope are considered.
2322

2423
1. Inherent methods, with receiver of type `A`, `&A`, `&mut A`.
2524
1. Trait methods with receiver of type `A`.
2625
1. Trait methods with receiver of type `&A`.
2726
1. Trait methods with receiver of type `&mut A`.
2827
1. If it's possible, Rust will then repeat steps 1-5 with
2928
`<A as std::ops::Deref>::Target`, and insert a dereference operator.
30-
1. If `A` is now an [array](types.html#array-and-slice-types) type, then
31-
repeat steps 1-4 with the corresponding slice type.
29+
1. If `A` is now an [array] type, then repeat steps 1-4 with the corresponding
30+
slice type.
3231

33-
Note: that in steps 1-4 the receiver is used, not the type of `Self` nor the
34-
type of `A`. For example
32+
Note: In steps 1-4, the receiver is used, not the type of `Self` nor the
33+
type of `A`. For example:
3534

3635
```rust,ignore
3736
// `Self` is `&A`, receiver is `&A`.
@@ -48,10 +47,21 @@ Another note: this process does not use the mutability or lifetime of the
4847
receiver, or whether `unsafe` methods can currently be called to resolve
4948
methods. These constraints instead lead to compiler errors.
5049

51-
If a step is reached where there is more than one possible method (where
52-
generic methods or traits are considered the same), then it is a compiler
53-
error. These cases require a [more specific
54-
syntax.](expressions/call-expr.html#disambiguating-function-calls) for method
50+
If a step is reached where there is more than one possible method, such as where
51+
generic methods or traits are considered the same, then it is a compiler
52+
error. These cases require a [disambiguating function call syntax] for method
5553
and function invocation.
5654

57-
[IDENTIFIER]: identifiers.html
55+
> Warning: For [trait objects], if there is an inherent method of the same name
56+
> as a trait method, it will give a compiler error when trying to call the
57+
> method in a method call expression. Instead, you can call the method using
58+
> [disambiguating function call syntax], in which case it calls the trait
59+
> method, not the inherent method. There is no way to call the inherent method.
60+
> Just don't define inherent methods on trait objects with the same name a trait
61+
> method and you'll be fine.
62+
63+
[IDENTIFIER]: identifiers.html
64+
[visible]: visibility-and-privacy.html
65+
[array]: types.html#array-and-slice-types
66+
[trait objects]: types.html#trait-objects
67+
[disambiguating function call syntax]: expressions/call-expr.html#disambiguating-function-calls

src/expressions/operator-expr.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,16 @@ let y = &mut 9;
8484
assert_eq!(*y, 11);
8585
```
8686

87-
## The `?` operator
87+
## The question mark operator
8888

89-
The `?` ("question mark") operator can be applied to values of the `Result<T,
90-
E>` type to propagate errors. If applied to `Err(e)` it will return
91-
`Err(From::from(e))` from the enclosing function or closure. If applied to
92-
`Ok(x)` it will unwrap the value to return `x`. Unlike other unary operators
93-
`?` is written in postfix notation. `?` cannot be overloaded.
89+
The question mark operator (`?`) unwraps valid values or returns errornous
90+
values, propagating them to the calling function. It is a unary postfix
91+
operator that can only be applied to the types `Result<T, E>` and `Option<T>`.
92+
93+
When applied to values of the `Result<T, E>` type, it propagates errors. If
94+
the value is `Err(e)`, then it will return `Err(From::from(e))` from the
95+
enclosing function or closure. If applied to `Ok(x)`, then it will unwrap the
96+
value to evaulate to `x`.
9497

9598
```rust
9699
# use std::num::ParseIntError;
@@ -105,6 +108,26 @@ println!("{:?}", res);
105108
# assert!(res.is_err())
106109
```
107110

111+
When applied to values of the `Option<T>` type, it propagates `Nones`. If the
112+
value is `None`, then it will return `None`. If applied to `Some(x)`, then it
113+
will unwrap the value to evaluate to `x`.
114+
115+
```rust
116+
fn try_option_some() -> Option<u8> {
117+
let val = Some(1)?;
118+
Some(val)
119+
}
120+
assert_eq!(try_option_some(), Some(1));
121+
122+
fn try_option_none() -> Option<u8> {
123+
let val = None?;
124+
Some(val)
125+
}
126+
assert_eq!(try_option_none(), None);
127+
```
128+
129+
`?` cannot be overloaded.
130+
108131
## Negation operators
109132

110133
These are the last two unary operators. This table summarizes the behavior of

src/glossary.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,4 @@ Generic functions and generic structs can use traits to constrain, or bound, the
137137
[trait objects]: types.html#trait-objects
138138
[implementations]: items/implementations.html
139139
[traits]: items/traits.html
140-
[object safety]: types.html#object-safety
141-
[trait objects]: types.html#trait-objects
140+
[object safety]: items/traits.html#object-safety

src/items/enumerations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,6 @@ enum ZeroVariants {}
138138
[_TupleFields_]: items/structs.html
139139
[_StructFields_]: items/structs.html
140140
[enumerated type]: types.html#enumerated-types
141-
[`mem::discriminant`]: std/mem/fn.discriminant.html
141+
[`mem::discriminant`]: ../std/mem/fn.discriminant.html
142142
[numeric cast]: expressions/operator-expr.html#semantics
143143
[`repr` attribute]: attributes.html#ffi-attributes

src/items/static-items.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ following are true:
114114
* Interior mutability is required.
115115

116116
[constant]: items/constant-items.html
117-
[interior mutable]: interior_mutability.html
117+
[interior mutable]: interior-mutability.html
118118
[IDENTIFIER]: identifiers.html
119119
[_Type_]: types.html
120120
[_Expression_]: expressions.html

src/items/traits.md

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -215,26 +215,18 @@ fn draw_figure<U: Shape>(surface: Surface, Figure(sh1, sh2): Figure<U>) {
215215
}
216216
```
217217

218-
## Trait objects
218+
## Object Safety
219219

220-
Traits also define a [trait object] with the same name as the trait. Values of
221-
this type are created by coercing from a pointer of some specific type to a
222-
pointer of trait type. For example, `&T` could be coerced to `&Shape` if `T:
223-
Shape` holds (and similarly for `Box<T>`). This coercion can either be implicit
224-
or [explicit]. Here is an example of an explicit coercion:
220+
Object safe traits can be the base trait of a [trait object]. A trait is
221+
*object safe* if it has the following qualities (defined in [RFC 255]):
225222

226-
```rust
227-
trait Shape { }
228-
impl Shape for i32 { }
229-
let mycircle = 0i32;
230-
let myshape: Box<Shape> = Box::new(mycircle) as Box<Shape>;
231-
```
232-
233-
The resulting value is a box containing the value that was cast, along with
234-
information that identifies the methods of the implementation that was used.
235-
Values with a trait type can have [methods called] on them, for any method in
236-
the trait, and can be used to instantiate type parameters that are bounded by
237-
the trait.
223+
* It must not require `Self: Sized`
224+
* All associated functions must either have a `where Self: Sized` bound or
225+
* Not have any type parameters (although lifetime parameters are allowed)
226+
* Must be a method: its first parameter must be called self, with type
227+
`Self`, `&Self`, `&mut Self`, `Box<Self>`.
228+
* `Self` may only be used in the type of the receiver.
229+
* It must not have any associated constants.
238230

239231
## Supertraits
240232

@@ -302,6 +294,11 @@ let mycircle = Box::new(mycircle) as Box<Circle>;
302294
let nonsense = mycircle.radius() * mycircle.area();
303295
```
304296

297+
[`Send`]: ../std/marker/trait.Send.html
298+
[`Send`]: ../std/marker/trait.Sync.html
299+
[`UnwindSafe`]: ../std/panic/trait.UnwindSafe.html
300+
[`RefUnwindSafe`]: ../std/panic/trait.RefUnwindSafe.html
305301
[trait object]: types.html#trait-objects
306302
[explicit]: expressions/operator-expr.html#type-cast-expressions
307-
[methods called]: expressions/method-call-expr.html
303+
[methods called]: expressions/method-call-expr.html
304+
[RFC 255]: https://github.com/rust-lang/rfcs/blob/master/text/0255-object-safety.md

src/paths.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Paths
22

3-
A _path_ is a sequence of one or more path components _logically_ separated by
4-
a namespace qualifier (`::`). If a path consists of only one component, it may
5-
refer to either an [item] or a [variable] in a local control
6-
scope. If a path has multiple components, it refers to an item.
3+
A *path* is a sequence of one or more path components _logically_ separated by
4+
a namespace qualifier (`::`). If a path consists of only one component, it
5+
refers to either an [item] or a [variable] in a local control
6+
scope. If a path has multiple components, it always refers to an item.
77

88
Two examples of simple paths consisting of only identifier components:
99

src/special-types-and-traits.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,19 +127,19 @@ compiler, not by [implementation items].
127127
[`Sync`]: ../std/marker/trait.Sync.html
128128

129129
[Arrays]: types.html#array-and-slice-types
130-
[call expressions]: expressoins/call-expr.html
130+
[call expressions]: expressions/call-expr.html
131131
[deref coercions]: type-coercions.html#coercion-types
132132
[dereference operator]: expressions/operator-expr.html#the-dereference-operator
133133
[destructor]: destructors.html
134134
[drop check]: ../nomicon/dropck.html
135135
[dynamically sized type]: dynamically-sized-types.html
136136
[Function pointers]: types.html#function-pointer-types
137137
[function item types]: types.html#function-item-types
138-
[implementation items]: items.html/implementations.html
138+
[implementation items]: items/implementations.html
139139
[indexing expressions]: expressions/array-expr.html#array-and-slice-indexing-expressions
140-
[interior mutability]: iterior-mutability.html
140+
[interior mutability]: interior-mutability.html
141141
[Numeric types]: types.html#numeric-types
142-
[Methods]: items.html#associated-functions-and-methods
142+
[Methods]: items/traits.html#associated-functions-and-methods
143143
[method resolution]: expressions/method-call-expr.html
144144
[operators]: expressions/operator-expr.html
145145
[orphan rules]: items/implementations.html#trait-implementation-coherence

src/statements.md

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,48 @@
11
# Statements
22

3-
A _statement_ is a component of a block, which is in turn a component of an
4-
outer [expression](expressions.html) or [function](items/functions.html).
3+
A *statement* is a component of a [block], which is in turn a component of an
4+
outer [expression] or [function].
55

66
Rust has two kinds of statement: [declaration
77
statements](#declaration-statements) and [expression
88
statements](#expression-statements).
99

1010
## Declaration statements
1111

12-
A _declaration statement_ is one that introduces one or more *names* into the
12+
A *declaration statement* is one that introduces one or more *names* into the
1313
enclosing statement block. The declared names may denote new variables or new
14-
items.
14+
[items][item].
15+
16+
The two kinds of declaration statements are item declarations and `let`
17+
statements.
1518

1619
### Item declarations
1720

18-
An _item declaration statement_ has a syntactic form identical to an
19-
[item](items.html) declaration within a module. Declaring an item &mdash; a
20-
function, enumeration, struct, type, static, trait, implementation or module
21-
&mdash; locally within a statement block is simply a way of restricting its
22-
scope to a narrow region containing all of its uses; it is otherwise identical
23-
in meaning to declaring the item outside the statement block.
21+
An *item declaration statement* has a syntactic form identical to an
22+
[item declaration][item] within a [module]. Declaring an item within a statement
23+
block restricts its scope to the block containing the statement. The item is not
24+
given a [canonical path] nor are any sub-items it may declare. The exception to
25+
this is that associated items defined by [implementations] are still accessible
26+
in outer scopes as long as the item and, if applicable, trait are accessible.
27+
It is otherwise identical in meaning to declaring the item inside a module.
28+
29+
There is no implicit capture of the containing function's generic parameters,
30+
parameters, and local variables. For example, `inner` may not access
31+
`outer_var`.
32+
33+
```rust
34+
fn outer() {
35+
let outer_var = true;
2436

25-
> **Note**: there is no implicit capture of the function's dynamic environment when
26-
> declaring a function-local item.
37+
fn inner() { /* outer_var is not in scope here */ }
38+
39+
inner();
40+
}
41+
```
2742

2843
### `let` statements
2944

30-
A _`let` statement_ introduces a new set of variables, given by a pattern. The
45+
A *`let` statement* introduces a new set of variables, given by a pattern. The
3146
pattern may be followed by a type annotation, and/or an initializer expression.
3247
When no type annotation is given, the compiler will infer the type, or signal
3348
an error if insufficient type information is available for definite inference.
@@ -36,13 +51,15 @@ declaration until the end of the enclosing block scope.
3651

3752
## Expression statements
3853

39-
An _expression statement_ is one that evaluates an
40-
[expression](expressions.html) and ignores its result. As a rule, an expression
41-
statement's purpose is to trigger the effects of evaluating its expression.
42-
An expression that consists of only a [block
43-
expression](expressions/block-expr.html) or control flow expression,
44-
that doesn't end a block and evaluates to `()` can also be used as an
45-
expression statement by omitting the trailing semicolon.
54+
An *expression statement* is one that evaluates an [expression] and ignores its
55+
result. As a rule, an expression statement's purpose is to trigger the effects
56+
of evaluating its expression.
57+
58+
An expression that consists of only a [block expression][block] or control flow
59+
expression, if used in a context where a statement is permitted, can omit the
60+
trailing semicolon. This can cause an ambiguity between it being parsed as a
61+
standalone statement and as a part of another expression; in this case, it is
62+
parsed as a statement.
4663

4764
```rust
4865
# let mut v = vec![1, 2, 3];
@@ -54,3 +71,28 @@ if v.is_empty() {
5471
} // Semicolon can be omitted.
5572
[1]; // Separate expression statement, not an indexing expression.
5673
```
74+
75+
When the trailing semicolon is omitted, the result must be type `()`.
76+
77+
```rust
78+
// bad: the block's type is i32, not ()
79+
// Error: expected `()` because of default return type
80+
// if true {
81+
// 1
82+
// }
83+
84+
// good: the block's type is i32
85+
if true {
86+
1
87+
} else {
88+
2
89+
};
90+
```
91+
92+
[block]: expressions/block-expr.html
93+
[expression]: expressions.html
94+
[function]: items/functions.html
95+
[item]: items.html
96+
[module]: items/modules.html
97+
[canonical path]: paths.html#canonical-paths
98+
[implementations]: items/implementations.html

src/tokens.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ literal_. The grammar for recognizing the two kinds of literals is mixed.
318318
>
319319
> INTEGER_SUFFIX :
320320
> &nbsp;&nbsp; &nbsp;&nbsp; `u8` | `u16` | `u32` | `u64` | `usize`
321-
> &nbsp;&nbsp; | `i8` | `u16` | `i32` | `i64` | `usize`
321+
> &nbsp;&nbsp; | `i8` | `i16` | `i32` | `i64` | `isize`
322322
323323
<!-- FIXME: separate the DECIMAL_LITERAL with no prefix or suffix (used on tuple indexing and float_literal -->
324324
<!-- FIXME: u128 and i128 -->

src/type-layout.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ padding bytes and forcing the alignment of the type to `1`.
281281
[`size_of`]: ../std/mem/fn.size_of.html
282282
[`Sized`]: ../std/marker/trait.Sized.html
283283
[dynamically sized types]: dynamically-sized-types.html
284-
[C-like enumerations]: items/enumerations.html#c-like-enumerations
285-
[zero-variant enumerations]: items/enumerations.html#zero-variant-enumerations
284+
[C-like enumerations]: items/enumerations.html#custom-discriminant-values-for-field-less-enumerations
285+
[zero-variant enumerations]: items/enumerations.html#zero-variant-enums
286286
[undefined behavior]: behavior-considered-undefined.html
287287
[27060]: https://github.com/rust-lang/rust/issues/27060

0 commit comments

Comments
 (0)