RS.CLIPPY.INDEXING_SLICING

Indexing/slicing usage

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

What it does

Checks for usage of indexing or slicing that may panic at runtime.

This lint does not report on indexing or slicing operations that always panic, clippy's out_of_bound_indexing already handles those cases.

Why restrict this?

To avoid implicit panics from indexing and slicing.

There are "checked" alternatives which do not panic, and can be used with unwrap() to make an explicit panic when it is desired.

Limitations

This lint does not check for the usage of indexing or slicing on strings. These are covered by the more specific string_slice lint.

Example

// Vector
let x = vec![0, 1, 2, 3];

x[2];
x[100];
&x[2..100];

// Array
let y = [0, 1, 2, 3];

let i = 10; // Could be a runtime value
let j = 20;
&y[i..j];

Use instead:

x.get(2);
x.get(100);
x.get(2..100);

let i = 10;
let j = 20;
y.get(i..j);

Configuration

  • allow-indexing-slicing-in-tests: Whether indexing_slicing should be allowed in test functions or #[cfg(test)]

    (default: false)

  • suppress-restriction-lint-in-const: Whether to suppress a restriction lint in constant code. In same cases the restructured operation might not be unavoidable, as the suggested counterparts are unavailable in constant code. This configuration will cause restriction lints to trigger even if no suggestion can be made.

    (default: false)