RS.CLIPPY.MANUAL_STRIP

Suggests using `strip_{prefix,suffix}` over `str::{starts,ends}_with` and slicing

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_strip. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Suggests using strip_{prefix,suffix} over str::{starts,ends}_with and slicing using the pattern's length.

Why is this bad?

Using str:strip_{prefix,suffix} is safer and may have better performance as there is no slicing which may panic and the compiler does not need to insert this panic code. It is also sometimes more readable as it removes the need for duplicating or storing the pattern used by str::{starts,ends}_with and in the slicing.

Example

let s = "hello, world!";
if s.starts_with("hello, ") {
    assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
}

Use instead:

let s = "hello, world!";
if let Some(end) = s.strip_prefix("hello, ") {
    assert_eq!(end.to_uppercase(), "WORLD!");
}

Configuration

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

    (default: current version)