RS.CLIPPY.MANUAL_OPTION_AS_SLICE

Manual `Option::as_slice`

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

What it does

This detects various manual reimplementations of Option::as_slice.

Why is this bad?

Those implementations are both more complex than calling as_slice and unlike that incur a branch, pessimizing performance and leading to more generated code.

Example

_ = opt.as_ref().map_or(&[][..], std::slice::from_ref);
_ = match opt.as_ref() {
    Some(f) => std::slice::from_ref(f),
    None => &[],
};

Use instead:

_ = opt.as_slice();
_ = opt.as_slice();

Configuration

  • msrv: The minimum rust version that the project supports. Defaults to the rust-version field in Cargo.toml

    (default: current version)