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
<h1><aclass="header" href="#when-to-add-must_use" id="when-to-add-must_use">When to add <code>#[must_use]</code></a></h1>
171
171
<p>The <code>#[must_use]</code> attribute can be applied to types or functions when failing to explicitly consider them or their output is almost certainly a bug.</p>
172
172
<p>As an example, <code>Result</code> is <code>#[must_use]</code> because failing to consider it may indicate a caller didn't realise a method was fallible:</p>
</span>// Is `check_status` infallible? Or did we forget to look at its `Result`?
173
+
<pre><codeclass="language-rust ignore">// Is `check_status` infallible? Or did we forget to look at its `Result`?
177
174
check_status();
178
-
<spanclass="boring">}
179
-
</span></code></pre></pre>
175
+
</code></pre>
180
176
<p>Operators like <code>saturating_add</code> are also <code>#[must_use]</code> because failing to consider their output might indicate a caller didn't realise they don't mutate the left-hand-side:</p>
</span>// A caller might assume this method mutates `a`
177
+
<pre><codeclass="language-rust ignore">// A caller might assume this method mutates `a`
185
178
a.saturating_add(b);
186
-
<spanclass="boring">}
187
-
</span></code></pre></pre>
179
+
</code></pre>
188
180
<p>Combinators produced by the <code>Iterator</code> trait are <code>#[must_use]</code> because failing to use them might indicate a caller didn't realize <code>Iterator</code>s are lazy and won't actually do anything unless you drive them:</p>
</span>// A caller might not realise this code won't do anything
181
+
<pre><codeclass="language-rust ignore">// A caller might not realise this code won't do anything
193
182
// unless they call `collect`, `count`, etc.
194
183
v.iter().map(|x| println!("{}", x));
195
-
<spanclass="boring">}
196
-
</span></code></pre></pre>
184
+
</code></pre>
197
185
<p>On the other hand, <code>thread::JoinHandle</code> isn't <code>#[must_use]</code> 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:</p>
<p>Look for any legitimate use-cases where <code>#[must_use]</code> will cause callers to explicitly ignore values. If these are common then <code>#[must_use]</code> probably isn't appropriate.</p>
208
192
<p>The <code>#[must_use]</code> attribute only produces warnings, so it can technically be introduced at any time. To avoid accumulating nuisance warnings though ping <code>@rust-lang/libs</code> for input before adding new <code>#[must_use]</code> attributes to existing types and functions.</p>
<h1><aclass="header" href="#when-to-add-must_use" id="when-to-add-must_use">When to add <code>#[must_use]</code></a></h1>
310
310
<p>The <code>#[must_use]</code> attribute can be applied to types or functions when failing to explicitly consider them or their output is almost certainly a bug.</p>
311
311
<p>As an example, <code>Result</code> is <code>#[must_use]</code> because failing to consider it may indicate a caller didn't realise a method was fallible:</p>
</span>// Is `check_status` infallible? Or did we forget to look at its `Result`?
312
+
<pre><codeclass="language-rust ignore">// Is `check_status` infallible? Or did we forget to look at its `Result`?
316
313
check_status();
317
-
<spanclass="boring">}
318
-
</span></code></pre></pre>
314
+
</code></pre>
319
315
<p>Operators like <code>saturating_add</code> are also <code>#[must_use]</code> because failing to consider their output might indicate a caller didn't realise they don't mutate the left-hand-side:</p>
</span>// A caller might assume this method mutates `a`
316
+
<pre><codeclass="language-rust ignore">// A caller might assume this method mutates `a`
324
317
a.saturating_add(b);
325
-
<spanclass="boring">}
326
-
</span></code></pre></pre>
318
+
</code></pre>
327
319
<p>Combinators produced by the <code>Iterator</code> trait are <code>#[must_use]</code> because failing to use them might indicate a caller didn't realize <code>Iterator</code>s are lazy and won't actually do anything unless you drive them:</p>
</span>// A caller might not realise this code won't do anything
320
+
<pre><codeclass="language-rust ignore">// A caller might not realise this code won't do anything
332
321
// unless they call `collect`, `count`, etc.
333
322
v.iter().map(|x| println!("{}", x));
334
-
<spanclass="boring">}
335
-
</span></code></pre></pre>
323
+
</code></pre>
336
324
<p>On the other hand, <code>thread::JoinHandle</code> isn't <code>#[must_use]</code> 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:</p>
<p>Look for any legitimate use-cases where <code>#[must_use]</code> will cause callers to explicitly ignore values. If these are common then <code>#[must_use]</code> probably isn't appropriate.</p>
347
331
<p>The <code>#[must_use]</code> attribute only produces warnings, so it can technically be introduced at any time. To avoid accumulating nuisance warnings though ping <code>@rust-lang/libs</code> for input before adding new <code>#[must_use]</code> attributes to existing types and functions.</p>
0 commit comments