Skip to content

Commit cfcae23

Browse files
authored
Merge branch 'master' into formatting-fixes
2 parents ee177e2 + b6d4ed7 commit cfcae23

22 files changed

+221
-85
lines changed

src/attributes.md

+112-5
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,6 @@ macro scope.
166166
object file that this item's contents will be placed into.
167167
- `no_mangle` - on any item, do not apply the standard name mangling. Set the
168168
symbol for this item to its identifier.
169-
- `must_use` - on structs and enums, will warn if a value of this type isn't used or
170-
assigned to a variable. You may also include an optional message by using
171-
`#[must_use = "message"]` which will be given alongside the warning.
172169

173170
### Deprecation
174171

@@ -325,7 +322,7 @@ The lint checks supported by the compiler can be found via `rustc -W help`,
325322
along with their default settings. [Compiler
326323
plugins][unstable book plugin] can provide additional lint checks.
327324

328-
```rust,ignore
325+
```rust
329326
pub mod m1 {
330327
// Missing documentation is ignored here
331328
#[allow(missing_docs)]
@@ -366,7 +363,7 @@ pub mod m2{
366363
This example shows how one can use `forbid` to disallow uses of `allow` for
367364
that lint check:
368365

369-
```rust,ignore
366+
```rust,compile_fail
370367
#[forbid(missing_docs)]
371368
pub mod m3 {
372369
// Attempting to toggle warning signals an error here
@@ -376,6 +373,105 @@ pub mod m3 {
376373
}
377374
```
378375

376+
#### `must_use` Attribute
377+
378+
The `must_use` attribute can be used on user-defined composite types
379+
([`struct`s][struct], [`enum`s][enum], and [`union`s][union]) and [functions].
380+
381+
When used on user-defined composite types, if the [expression] of an
382+
[expression statement] has that type, then the `unused_must_use` lint is
383+
violated.
384+
385+
```rust
386+
#[must_use]
387+
struct MustUse {
388+
// some fields
389+
}
390+
391+
# impl MustUse {
392+
# fn new() -> MustUse { MustUse {} }
393+
# }
394+
#
395+
fn main() {
396+
// Violates the `unused_must_use` lint.
397+
MustUse::new();
398+
}
399+
```
400+
401+
When used on a function, if the [expression] of an
402+
[expression statement] is a [call expression] to that function, then the
403+
`unused_must_use` lint is violated. The exceptions to this is if the return type
404+
of the function is `()`, `!`, or a [zero-variant enum], in which case the
405+
attribute does nothing.
406+
407+
```rust
408+
#[must_use]
409+
fn five() -> i32 { 5i32 }
410+
411+
fn main() {
412+
// Violates the unused_must_use lint.
413+
five();
414+
}
415+
```
416+
417+
When used on a function in a trait declaration, then the behavior also applies
418+
when the call expression is a function from an implementation of the trait.
419+
420+
```rust
421+
trait Trait {
422+
#[must_use]
423+
fn use_me(&self) -> i32;
424+
}
425+
426+
impl Trait for i32 {
427+
fn use_me(&self) -> i32 { 0i32 }
428+
}
429+
430+
fn main() {
431+
// Violates the `unused_must_use` lint.
432+
5i32.use_me();
433+
}
434+
```
435+
436+
When used on a function in an implementation, the attribute does nothing.
437+
438+
> Note: Trivial no-op expressions containing the value will not violate the
439+
> lint. Examples include wrapping the value in a type that does not implement
440+
> [`Drop`] and then not using that type and being the final expression of a
441+
> [block expression] that is not used.
442+
>
443+
> ```rust
444+
> #[must_use]
445+
> fn five() -> i32 { 5i32 }
446+
>
447+
> fn main() {
448+
> // None of these violate the unused_must_use lint.
449+
> (five(),);
450+
> Some(five());
451+
> { five() };
452+
> if true { five() } else { 0i32 };
453+
> match true {
454+
> _ => five()
455+
> };
456+
> }
457+
> ```
458+
459+
> Note: It is idiomatic to use a [let statement] with a pattern of `_`
460+
> when a must-used value is purposely discarded.
461+
>
462+
> ```rust
463+
> #[must_use]
464+
> fn five() -> i32 { 5i32 }
465+
>
466+
> fn main() {
467+
> // Does not violate the unused_must_use lint.
468+
> let _ = five();
469+
> }
470+
> ```
471+
472+
The `must_use` attribute may also include a message by using
473+
`#[must_use = "message"]`. The message will be given alongside the warning.
474+
379475
### Inline attribute
380476
381477
The inline attribute suggests that the compiler should place a copy of
@@ -430,4 +526,15 @@ You can implement `derive` for your own type through [procedural macros].
430526
[Doc comments]: comments.html#doc-comments
431527
[The Rustdoc Book]: ../rustdoc/the-doc-attribute.html
432528
[procedural macros]: procedural-macros.html
529+
[struct]: items/structs.html
530+
[enum]: items/enumerations.html
531+
[union]: items/unions.html
532+
[functions]: items/functions.html
533+
[expression]: expressions.html
534+
[expression statement]: statements.html#expression-statements
535+
[call expression]: expressions/call-expr.html
536+
[block expression]: expressions/block-expr.html
537+
[`Drop`]: special-types-and-traits.html#drop
538+
[let statement]: statements.html#let-statements
433539
[unstable book plugin]: ../unstable-book/language-features/plugin.html#lint-plugins
540+
[zero-variant enum]: enumerations.html#zero-variant-enums

src/destructors.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
When an [initialized] [variable] in Rust goes out of scope or a [temporary]
44
is no longer needed its _destructor_ is run. [Assignment] also runs the
5-
destructor of its left-hand operand, unless it's an unitialized variable. If a
5+
destructor of its left-hand operand, unless it's an uninitialized variable. If a
66
[struct] variable has been partially initialized, only its initialized fields
77
are dropped.
88

9-
The destrutor of a type consists of
9+
The destructor of a type consists of
1010

1111
1. Calling its [`std::ops::Drop::drop`] method, if it has one.
1212
2. Recursively running the destructor of all of its fields.

src/dynamically-sized-types.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ types">DSTs</abbr>. Such types can only be used in certain cases:
1414
type arguments when a bound of `?Sized`. By default any type parameter
1515
has a `Sized` bound.
1616
* Traits may be implemented for <abbr title="dynamically sized
17-
types">DSTs</abbr>. Unlike type parameters`Self: ?Sized` by default in trait
17+
types">DSTs</abbr>. Unlike type parameters `Self: ?Sized` by default in trait
1818
definitions.
1919
* Structs may contain a <abbr title="dynamically sized type">DST</abbr> as the
2020
last field, this makes the struct itself a

src/expressions.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ and blocks again can recursively nest inside each other to an arbitrary depth.
4444
## Expression precedence
4545

4646
The precedence of Rust operators and expressions is ordered as follows, going
47-
from strong to weak. Binary Operators at the same precedence level are
48-
evaluated in the order given by their associativity.
47+
from strong to weak. Binary Operators at the same precedence level are grouped
48+
in the order given by their associativity.
4949

5050
| Operator/Expression | Associativity |
5151
|-----------------------------|---------------------|
@@ -55,7 +55,7 @@ evaluated in the order given by their associativity.
5555
| Function calls, array indexing | |
5656
| `?` | |
5757
| Unary `-` `*` `!` `&` `&mut` | |
58-
| `as` `:` | left to right |
58+
| `as` | left to right |
5959
| `*` `/` `%` | left to right |
6060
| `+` `-` | left to right |
6161
| `<<` `>>` | left to right |
@@ -66,7 +66,6 @@ evaluated in the order given by their associativity.
6666
| `&&` | left to right |
6767
| <code>&#124;&#124;</code> | left to right |
6868
| `..` `..=` | Require parentheses |
69-
| `<-` | right to left |
7069
| `=` `+=` `-=` `*=` `/=` `%=` <br> `&=` <code>&#124;=</code> `^=` `<<=` `>>=` | right to left |
7170
| `return` `break` closures | |
7271

src/expressions/match-expr.md

+20-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> _MatchArmPatterns_ _MatchArmGuard_<sup>?</sup>
1919
>
2020
> _MatchArmPatterns_ :
21-
> &nbsp;&nbsp; `|`<sup>?</sup> _Pattern_ ( `|` _Pattern_ )<sup>*</sup>
21+
> &nbsp;&nbsp; `|`<sup>?</sup> _Pattern_ ( `|` _Pattern_ )<sup>\*</sup>
2222
>
2323
> _MatchArmGuard_ :
2424
> &nbsp;&nbsp; `if` [_Expression_]
@@ -140,6 +140,25 @@ backwards compatibility.
140140
Range patterns only work [`char`] and [numeric types]. A range pattern may not
141141
be a sub-range of another range pattern inside the same `match`.
142142

143+
Slice patterns can match both arrays of fixed size and slices of dynamic size.
144+
```rust
145+
// Fixed size
146+
let arr = [1, 2, 3];
147+
match arr {
148+
[1, _, _] => "starts with one",
149+
[a, b, c] => "starts with something else",
150+
};
151+
```
152+
```rust
153+
// Dynamic size
154+
let v = vec![1, 2, 3];
155+
match v[..] {
156+
[a, b] => { /* this arm will not apply because the length doesn't match */ }
157+
[a, b, c] => { /* this arm will apply */ }
158+
_ => { /* this wildcard is required, since we don't know length statically */ }
159+
}
160+
```
161+
143162
Finally, match patterns can accept *pattern guards* to further refine the
144163
criteria for matching a case. Pattern guards appear after the pattern and
145164
consist of a bool-typed expression following the `if` keyword. A pattern guard
@@ -164,4 +183,3 @@ let message = match maybe_digit {
164183
[numeric types]: types.html#numeric-types
165184
[_InnerAttribute_]: attributes.html
166185
[_OuterAttribute_]: attributes.html
167-
[range]: range-expr.html

src/expressions/method-call-expr.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
A _method call_ consists of an expression (the *receiver*) followed by a single
44
dot, an [identifier], and a parenthesized expression-list. Method calls are
5-
resolved to methods on specific traits, either statically dispatching to a
6-
method if the exact `self`-type of the left-hand-side is known, or dynamically
7-
dispatching if the left-hand-side expression is an indirect [trait
8-
object](types.html#trait-objects).
5+
resolved to associated [methods] on specific traits, either statically
6+
dispatching to a method if the exact `self`-type of the left-hand-side is known,
7+
or dynamically dispatching if the left-hand-side expression is an indirect
8+
[trait object](types.html#trait-objects).
99

1010
```rust
1111
let pi: Result<f32, _> = "3.14".parse();
@@ -95,3 +95,4 @@ and function invocation.
9595
[trait objects]: types.html#trait-objects
9696
[disambiguate call]: expressions/call-expr.html#disambiguating-function-calls
9797
[dereference]: expressions/operator-expr.html#the-dereference-operator
98+
[methods]: items/associated-items.html#methods

src/expressions/operator-expr.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,14 @@ assert_eq!(*y, 11);
109109
> _ErrorPropagationExpression_ :
110110
> &nbsp;&nbsp; [_Expression_] `?`
111111
112-
The question mark operator (`?`) unwraps valid values or returns errornous
112+
The question mark operator (`?`) unwraps valid values or returns erroneous
113113
values, propagating them to the calling function. It is a unary postfix
114114
operator that can only be applied to the types `Result<T, E>` and `Option<T>`.
115115

116116
When applied to values of the `Result<T, E>` type, it propagates errors. If
117117
the value is `Err(e)`, then it will return `Err(From::from(e))` from the
118118
enclosing function or closure. If applied to `Ok(x)`, then it will unwrap the
119-
value to evaulate to `x`.
119+
value to evaluate to `x`.
120120

121121
```rust
122122
# use std::num::ParseIntError;
@@ -393,7 +393,7 @@ An _assignment expression_ consists of a [place expression] followed by an
393393
equals sign (`=`) and a [value expression].
394394

395395
Evaluating an assignment expression [drops](destructors.html) the left-hand
396-
operand, unless it's an unitialized local variable or field of a local variable,
396+
operand, unless it's an uninitialized local variable or field of a local variable,
397397
and [either copies or moves](expressions.html#moved-and-copied-types) its
398398
right-hand operand to its left-hand operand. The left-hand operand must be a
399399
place expression: using a value expression results in a compiler error, rather
@@ -444,7 +444,7 @@ assert_eq!(x, 14);
444444

445445
[_BorrowExpression_]: #borrow-operators
446446
[_DereferenceExpression_]: #the-dereference-operator
447-
[_ErrorPropagationExpression_]: #the--operator
447+
[_ErrorPropagationExpression_]: #the-question-mark-operator
448448
[_NegationExpression_]: #negation-operators
449449
[_ArithmeticOrLogicalExpression_]: #arithmetic-and-logical-binary-operators
450450
[_ComparisonExpression_]: #comparison-operators

src/items/associated-items.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ The identifier if the name of the function. The generics, parameter list,
3333
return type, and where clause of the associated function must be the same as the
3434
associated function declarations's.
3535

36-
An *associated function definiton* defines a function associated with another
36+
An *associated function definition* defines a function associated with another
3737
type. It is written the same as a [function item].
3838

3939
An example of a common associated function is a `new` function that returns
@@ -77,15 +77,19 @@ let _: f64 = <f64 as Num>::from_i32(42);
7777
let _: f64 = f64::from_i32(42);
7878
```
7979

80+
### Methods
81+
8082
Associated functions whose first parameter is named `self` are called *methods*
8183
and may be invoked using the [method call operator], for example, `x.foo()`, as
8284
well as the usual function call notation.
8385

84-
When the first parameter is named `self`, the following shorthands may be used.
86+
The `self` parameter must have one of the following types. As a result, the
87+
following shorthands may be used to declare `self`:
8588

8689
* `self` -> `self: Self`
8790
* `&'lifetime self` -> `self: &'lifetime Self`
8891
* `&'lifetime mut self` -> `self: &'lifetime mut Self`
92+
* `self : Box<Self>` (no shorthand)
8993

9094
> Note: Lifetimes can be and usually are elided with this shorthand.
9195

src/items/constant-items.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fn create_and_drop_zero_with_destructor() {
6161
```
6262

6363
[constant value]: expressions.html#constant-expressions
64-
[static lifetime elision]: items/lifetime-elision.html#static-lifetime-elision
64+
[static lifetime elision]: lifetime-elision.html#static-lifetime-elision
6565
[`Drop`]: special-types-and-traits.html#drop
6666
[IDENTIFIER]: identifiers.html
6767
[_Type_]: types.html

src/items/functions.md

+7-9
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,10 @@ as parameters, through which the caller passes arguments into the function, and
77
the *output* [*type*][type] of the value the function will return to its caller
88
on completion.
99

10-
[block]: expressions/block-expr.html
11-
[variables]: variables.html
12-
[type]: types.html
13-
1410
When referred to, a _function_ yields a first-class *value* of the
1511
corresponding zero-sized [*function item type*], which
1612
when called evaluates to a direct call to the function.
1713

18-
[*function item type*]: types.html#function-item-types
19-
2014
For example, this is a simple function:
2115
```rust
2216
fn answer_to_life_the_universe_and_everything() -> i32 {
@@ -61,7 +55,7 @@ fn foo<A, B>(x: A, y: B) {
6155
```
6256

6357
Inside the function signature and body, the name of the type parameter can be
64-
used as a type name. [Trait](items/traits.html) bounds can be specified for type
58+
used as a type name. [Trait] bounds can be specified for type
6559
parameters to allow methods with that trait to be called on values of that
6660
type. This is specified using the `where` syntax:
6761

@@ -91,8 +85,6 @@ component after the function name. This might be necessary if there is not
9185
sufficient context to determine the type parameters. For example,
9286
`mem::size_of::<u32>() == 4`.
9387

94-
[path]: paths.html
95-
9688
## Extern functions
9789

9890
Extern functions are part of Rust's foreign function interface, providing the
@@ -124,3 +116,9 @@ of an extern function will cause the process to abort. In LLVM, this is
124116
implemented by executing an illegal instruction.
125117

126118
[external blocks]: items/external-blocks.html
119+
[path]: paths.html
120+
[block]: expressions/block-expr.html
121+
[variables]: variables.html
122+
[type]: types.html
123+
[*function item type*]: types.html#function-item-types
124+
[Trait]: items/traits.html

0 commit comments

Comments
 (0)