RS.CLIPPY.UNWRAP_OR_DEFAULT

Using `.unwrap_or`, etc. with an argument that constructs a default value

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: unwrap_or_default. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks for usages of the following functions with an argument that constructs a default value (e.g., Default::default or String::new):

  • unwrap_or
  • unwrap_or_else
  • or_insert
  • or_insert_with

Why is this bad?

Readability. Using unwrap_or_default in place of unwrap_or/unwrap_or_else, or or_default in place of or_insert/or_insert_with, is simpler and more concise.

Known problems

In some cases, the argument of unwrap_or, etc. is needed for type inference. The lint uses a heuristic to try to identify such cases. However, the heuristic can produce false negatives.

Examples

x.unwrap_or(Default::default());
map.entry(42).or_insert_with(String::new);

Use instead:

x.unwrap_or_default();
map.entry(42).or_default();

Past names

  • unwrap_or_else_default