rust-lang / rust-lang/rust-clippy

Suggest usage of `array.as_slice()` (instead of `&array[..]`) and `array.as_mut_slice()`

Open
#7,633 6 comments 2 reactions 1 assignee View on GitHub

@nils-degroot is already working on this.

Since May 15, 2025.

A-lint good first issue L-restriction
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Lints for example this code:

let array: [u8; 4] = [1, 2, 3, 4];
let slice: &[u8] = &array[..];

and suggests array::as_slice:

let array: [u8; 4] = [1, 2, 3, 4];
let slice: &[u8] = array.as_slice();

(same should be suggested for &mut array[..], which can be replaced with array.as_mut_slice())

Categories (optional)
  • Kind: clippy::pedantic or clippy::style
What is the advantage of the recommended code over the original code?

For example:

  • easier to read for people new to rust (not very obvious what [..] does)
  • there must be a reason why array::as_slice exists? (see rust-lang/rust#76120)
Drawbacks
  • might have a lot of false positives or not much benefit in using the function
Possibly related issues:
  • #728
  • #5598
Stabilization

At the time of writing this issue, the function is still unstable, but with the merging of rust-lang/rust#88353, this feature will be stabilized. (final-comment-period, so will be merged in the next few days)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.