You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/code-considerations/design/must-use.md
+4-4Lines changed: 4 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -4,29 +4,29 @@ The `#[must_use]` attribute can be applied to types or functions when failing to
4
4
5
5
As an example, `Result` is `#[must_use]` because failing to consider it may indicate a caller didn't realise a method was fallible:
6
6
7
-
```rust
7
+
```rust,ignore
8
8
// Is `check_status` infallible? Or did we forget to look at its `Result`?
9
9
check_status();
10
10
```
11
11
12
12
Operators like `saturating_add` are also `#[must_use]` because failing to consider their output might indicate a caller didn't realise they don't mutate the left-hand-side:
13
13
14
-
```rust
14
+
```rust,ignore
15
15
// A caller might assume this method mutates `a`
16
16
a.saturating_add(b);
17
17
```
18
18
19
19
Combinators produced by the `Iterator` trait are `#[must_use]` because failing to use them might indicate a caller didn't realize `Iterator`s are lazy and won't actually do anything unless you drive them:
20
20
21
-
```rust
21
+
```rust,ignore
22
22
// A caller might not realise this code won't do anything
23
23
// unless they call `collect`, `count`, etc.
24
24
v.iter().map(|x| println!("{}", x));
25
25
```
26
26
27
27
On the other hand, `thread::JoinHandle` isn't `#[must_use]` because spawning fire-and-forget work is a legitimate pattern and forcing callers to explicitly ignore handles could be a nuisance rather than an indication of a bug:
0 commit comments