Rust

Rust

By DeepSource

Invoking `.map_or()` with identity closureRS-W1201

Anti-pattern
Autofix

Calling map_or with the identity closure is equivalent to using unwrap_or which is both easier to read and generally more performant. Consider replacing map_or with unwrap_or.

Examples

Bad practice

let foo = vec![1, 2, 3];
let bar = foo.get(1).map_or(0, |x| x);

Recommended

let foo = vec![1, 2, 3];
let bar = foo.get(1).unwrap_or(0);