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)
The
str::strip_circumfixdocs says:Similarly, the
slice::strip_circumfixdocs says:These documentation texts are problematic when the prefix and the suffix string overlap. For example:
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 returnSome.The same behavior occurs with slices.
Example code using slices instead of strings
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)