Skip to content

strip_circumfix behavior doesn't match docs when prefix and suffix overlap. #158388

Description

@theemathas

The str::strip_circumfix docs says:

If the string starts with the pattern prefix and ends with the pattern suffix, returns the substring after the prefix and before the suffix, wrapped in Some.

Similarly, the slice::strip_circumfix docs says:

If the slice starts with prefix and ends with suffix, returns the subslice after the prefix and before the suffix, wrapped in Some.

These documentation texts are problematic when the prefix and the suffix string overlap. For example:

fn main() {
    let s = "abcde";
    let stripped = s.strip_circumfix("abc", "cde");
    println!("{stripped:?}");
}

The above code outputs None. However, since the string does in fact start with the prefix, and end with the suffix, then arguably, the docs say that the method should return Some.

The same behavior occurs with slices.

Example code using slices instead of strings
fn main() {
    let s = [0, 1, 2, 3, 4];
    let stripped = s.strip_circumfix(&[0, 1, 2], &[2, 3, 4]);
    println!("{stripped:?}");
}

The above code outputs None.

I'm not sure whether the docs or the behavior is incorrect, though.

Note that these two methods has just been recently stabilized in nightly in the PR #158012 (tracking issue: #147946). Therefore, I'm nominating this issue for discussion.

Meta

Reproducible on the playground with version 1.98.0-nightly (2026-06-23 f28ac764c36004fa6a6e)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-sliceArea: `[T]`A-strArea: str and StringC-bugCategory: This is a bug.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions